Sortierfunktion aus algorithm funktioniert nicht



  • Hallo! Ich habe folgendes Problem:
    Ich habe eine Basis-Klasse: Copter; und drei davon abgeleitete Klassen.

    Ich speichere einzelne Copter in einem vector.
    nun will ich die eingegebenen Copter sortiert nach z.b. Preis ausgeben.

    Wenn ich dieses Programm hier verwende:
    http://forums.codeguru.com/showthread.php?366064-STL-Sorting-How-to-sort-a-std-vector-containing-classes-structures

    und starte, funktioniert das sortieren einwandfrei.
    Nur bei meinem Programm funktioniert nichts so wie es soll.

    Hier der Source Code:

    #include <iostream>
    #include <string>
    #include <vector>
    #include <algorithm>
    using namespace std;
    /*
    bool MySortFunction (Copter *d1, Copter *d2)
    {
    return d1->
    }
    */

    bool MySortFunction(const Copter& d1, const Copter& d2)
    {
    return d1.Preis < d2.Preis;
    }

    class Copter
    {
    public:

    string Hersteller;
    string Name;
    double Preis;
    string Kompatibilität;
    string Software;

    void setHersteller()
    {
    cout << " Hersteller: ";
    cin >> Hersteller;
    cout << endl;
    }
    void setName()
    {
    cout << " Name: ";
    cin >> Name;
    }
    void setPreis()
    {
    cout << "\n Preis eingeben: ";
    cin >> Preis;
    }

    void setKompatibilität()
    {
    cout << " Kompabilität (Tricopter bis Octocopter): ";
    cin >> Kompatibilität;
    cout << endl;
    }

    void setSoftware()
    {
    cout << " Verwendete Software: ";
    cin >> Software;
    cout << endl;
    }

    virtual void Eingabe() //Polymorphism
    {
    setHersteller();
    setName();
    setPreis();
    setKompatibilität();
    setSoftware();
    }

    virtual void Ausgabe() //Polymorphism
    {
    cout << "\n Hersteller: " << Hersteller << endl;
    cout << " Name: " << Name << endl;
    cout << " Preis: " << Preis << " Euro\n";
    cout << " Kompatibilitaet: " << Kompatibilität << endl;
    cout << " Software: " << Software << endl;
    }
    string getName() { return Name;};
    double getPreis() { return Preis;};

    Copter()
    {
    Hersteller = "";
    Name = "";
    Preis = 0;
    Kompatibilität = "";
    Software = "";
    }
    };

    class MilitaryCopter : public Copter
    {
    protected:
    string Geschütz;
    bool Tarnung;

    private:

    void setGeschütz()
    {
    cout << " Geschuetz: ";
    cin >> Geschütz;
    }

    void setTarnung()
    {
    cout << "\n Tarnung eingeben (0) Keine (1) mit Tarnung: ";
    cin >> Tarnung;
    }

    public:
    void Eingabe()
    {
    Copter::Eingabe();
    setGeschütz();
    setTarnung();
    }

    void Ausgabe()
    {
    Copter::Ausgabe();
    getGeschütz();
    getTarnung();
    }

    void getGeschütz()
    {
    cout << " Geschuetz oder Waffe: " << Geschütz << endl;
    }
    void getTarnung()
    {
    cout << " Tarnung: " << Tarnung << endl;
    }

    MilitaryCopter()
    {
    Geschütz = "Maschinengewehr";
    Tarnung = 1;
    }
    };

    class FotoCopter : public Copter
    {
    protected:
    bool Gimbal;
    double Nutzlast;
    bool Entkopplung;

    private:

    void setGimbal()
    {
    cout << " Gimbal vorhanden? (0) NEIN (1) JA: ";
    cin >> Gimbal;
    }

    void setNutzlast()
    {
    cout << "\n maximale Nutzlast in Gramm eingeben: ";
    cin >> Nutzlast;
    }

    void setEntkopplung()
    {
    cout << " Entkopplung vorhanden? (0) NEIN (1) JA: ";
    cin >> Entkopplung;
    }

    public:
    void Eingabe()
    {
    Copter::Eingabe();
    setGimbal();
    setNutzlast();
    setEntkopplung();
    }

    void Ausgabe()
    {
    Copter::Ausgabe();
    getGimbal();
    getNutzlast();
    getEntkopplung();
    }

    void getGimbal()
    {
    if(Gimbal = 1)
    cout << " Gimbal: Ja" << endl;
    else
    cout << " Gimbal: Nein" << endl;
    }

    void getNutzlast()
    {
    cout << " Nutzlast [gramm] : " << Nutzlast << endl;
    }

    void getEntkopplung()
    {
    if(Entkopplung = 1)
    cout << " Entkopplung: Ja" << endl;
    else
    cout << " Entkopplung: Nein" << endl;
    }

    FotoCopter()
    {
    Gimbal = 1;
    Nutzlast = 2000;
    Entkopplung = 1;
    }
    };

    class AcroCopter : public Copter
    {
    protected:
    string Propeller;
    double Leistung;
    bool FPVable;

    private:

    void setPropeller()
    {
    cout << " Welche Propeller werden verwendet: ";
    cin >> Propeller;
    }

    void setLeistung()
    {
    cout << "\n maximale Leistung in Watt eingeben: ";
    cin >> Leistung;
    }

    void setFPV()
    {
    cout << " Ist der Copter FPV-faehig? (0) NEIN (1) JA: ";
    cin >> FPVable;
    }

    public:
    void Eingabe()
    {
    Copter::Eingabe();
    setPropeller();
    setLeistung();
    setFPV();
    }

    void Ausgabe()
    {
    Copter::Ausgabe();
    getPropeller();
    getLeistung();
    getFPV();
    }

    void getPropeller()
    {
    cout << " Propeller: " << Propeller << endl;
    }

    void getLeistung()
    {
    cout << " Leistung [Watt]: " << Leistung << endl;
    }

    void getFPV()
    {
    if(FPVable == 1)
    cout << " FPV-faehig: Ja" << endl;
    else
    cout << " FPV-faehig: Nein" << endl;
    }

    AcroCopter()
    {
    Propeller = "Standard";
    Leistung = 800;
    FPVable = 0;
    }
    };

    void main()
    {
    Copter c;
    vector<Copter
    > c1;
    vector<Copter*>::iterator iter;
    int auswahl, copterchoice, retry = 1;

    short counter = 0;
    short sort = 0;
    while(retry == 1)
    {
    cout << "\n (1) Neuen Copter anlegen\n (2) Automatisch sechs Copter erzeugen\n (3) Alle Copter\n (4) Copter sortieren und ausgeben\n (0) Programm beenden\n\n";
    cin >> auswahl;

    switch(auswahl)
    {

    case 0:
    retry = 0;
    break;

    case 1:
    cout << "\n Welchen Copter wollen Sie erstellen?\n";
    cout << " (1) Militarycopter\n (2) Fotocopter\n (3) Acrocopter\n";
    cin >> copterchoice;

    switch(copterchoice)
    {

    case 1:
    c = new MilitaryCopter;
    c->Eingabe();
    c1.push_back(c);
    counter++;
    break;

    case 2:
    c = new FotoCopter;
    c->Eingabe();
    c1.push_back(c);
    counter++;
    break;

    case 3:
    c = new AcroCopter;
    c->Eingabe();
    c1.push_back(c);
    counter++;
    break;
    }

    retry = 1;

    break;

    case 3:
    cout << "\n Alle Copter:\n\n";
    for(int i = 0; i < counter; i++)
    c1[i]->Ausgabe();

    retry = 1;
    break;

    case 4:
    cout << "\n Wonach wollen Sie die Copter sortieren?\n";
    cout << "(1) Preis\n (2) Kompatibilitaet\n";
    cin >> sort;

    std::sort(c1.begin(), c1.end(), MySortFunction);

    break;

    default:
    cout << "\n Falsche Eingabe, bitte erneut versuchen!\n\n";
    retry = 1;
    break;
    }
    }
    cout << "\n\n Programm wird beendet!\n\n";
    }

    Ganz oben ist meine Funktion, die "true/false" an die sort funktion aus algorithm zurückgibt.
    Ich bekomme aber beim compilieren folgene Fehlermeldungen:

    1>------ Erstellen gestartet: Projekt: Copter_Information_System, Konfiguration: Debug Win32 ------
    1> Main.cpp
    1>c:\users\walter\documents\visual studio 2010\projects\copter_information_system\copter_information_system\main.cpp(13): error C4430: Fehlender Typspezifizierer - int wird angenommen. Hinweis: "default-int" wird von C++ nicht unterstützt.
    1>c:\users\walter\documents\visual studio 2010\projects\copter_information_system\copter_information_system\main.cpp(13): error C2143: Syntaxfehler: Es fehlt ',' vor '&'
    1>c:\users\walter\documents\visual studio 2010\projects\copter_information_system\copter_information_system\main.cpp(15): error C2065: 'd1': nichtdeklarierter Bezeichner
    1>c:\users\walter\documents\visual studio 2010\projects\copter_information_system\copter_information_system\main.cpp(15): error C2228: Links von ".Preis" muss sich eine Klasse/Struktur/Union befinden.
    1> Typ ist ''unknown-type''
    1>c:\users\walter\documents\visual studio 2010\projects\copter_information_system\copter_information_system\main.cpp(15): error C2065: 'd2': nichtdeklarierter Bezeichner
    1>c:\users\walter\documents\visual studio 2010\projects\copter_information_system\copter_information_system\main.cpp(15): error C2228: Links von ".Preis" muss sich eine Klasse/Struktur/Union befinden.
    1> Typ ist ''unknown-type''
    ========== Erstellen: 0 erfolgreich, Fehler bei 1, 0 aktuell, 0 übersprungen ==========

    Kann mir jemand weiterhelfen? ich verzweifel hier noch 🙂
    Auch gegen einen Denkanstoß, wo mein Fehler liegt hab ich nichts, dann komm ich vielleicht doch noch selbst drauf? 🙂

    Danke schonmal und LG, Walter



  • Benutze bitte die C++-Tags unter dem 😞
    Der Compiler will dir sagen, dass in Zeile 13 die Funktion MySortFunction nicht weiß was ein Copter ist. Das solltest du davor deklarieren.


  • Mod

    Reihenfolge von Deklarationen beachten.
    In Zeile 13 ist der Bezeichner Copter noch gar nicht bekannt (Abhilfe: Forwardeklaration von Copter oder Verschiebung der Funktionsdeklaration).

    Und Zeile 15 hängt sogar von der Klassendefinition ab, die Funktionsdefinition kann folglich nicht vor der Klassendefinition stehen.



  • Vielen Dank!!! Ich wusste nicht dass es so einfach ist 😃

    Jetzt bekomme ich in der Zeile wo ich die sort-Funktion aufrufe folgenden Fehler:

    1>c:\users\walter\documents\visual studio 2010\projects\copter_information_system\copter_information_system\main.cpp(355): error C2064: Ausdruck ergibt keine Funktion, die 3 Argumente übernimmt

    Was will mir der Compiler diesmal sagen?

    Danke für eure blitzschnelle Hilfe!!



  • Ich glaube den Fehler gefunden zu haben.
    Ich habe jetzt statt:
    sort(c1.begin(), c1.end(), MySortFunction);

    so geschrieben:
    std::sort(c1.begin(), c1.end(), MySortFunction);

    nun bekomme ich folgende Fehlermeldung:

    1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm(3720): error C2664: 'bool (const Copter &,const Copter &)': Konvertierung des Parameters 2 von 'Copter *' in 'const Copter &' nicht möglich

    Ich weiß auch zirka was das bedeutet, nur hab ich keine ahnung wie ich das Problem beheben kann?



  • MySortFunction sortiert Copter. c1 enthält Copter *. Das ist nicht dasselbe, also krachts. Lass entweder MySortFunction Copter * sortieren oder mach dass c1 Copter enthält. Ich würde letzteres empfehlen, dann fallen auch die unnötigen Aufrufe von new weg, die Aufrufe von delete hast du eh schon vergessen.



  • nwp3 schrieb:

    ... mach dass c1 Copter enthält. Ich würde letzteres empfehlen, dann fallen auch die unnötigen Aufrufe von new weg, die Aufrufe von delete hast du eh schon vergessen.

    Das wird nicht funktionieren, da abgeleitete Klassen benutzt werden.



  • JAWOHL! es Funktioniert!! 🙂
    Vielen Dank an alle!!!

    So hab ichs jetzt gemacht:

    bool MySortFunction(const Copter* d1, const Copter* d2)
    {
    return d1->Preis < d2->Preis;
    }

    mit Pointern hats hingehauen.

    Danke nochmal! Schönen abend noch 🙂


Anmelden zum Antworten