Seltsames Ausgabe verhalten (konsole)
-
Hab ein Problem mit meinem Programm. Die Ausgabe ist weitgehend korrekt , sollange nicht mehr als 3 objekte dargestellt werden sollen bei mehr kommt es zu seltsamen Ausgaben oder garkeiner Ausgabe, seltsam ist auch dass wenn man die Zeile mit dem "Mercedes" nicht auskommentiert und dafür den "volvo" auskommentiert das Feld wo eigentlich Mercedes stehen müsste leer bleibt.
kann mir da jemand helfen?Unit1.cpp
//--------------------------------------------------------------------------- #include <iostream> #include <conio.h> #include <list> #pragma hdrstop using namespace std; #include "KFZ.h" typedef bool func(KFZ*,KFZ*); bool lower(KFZ* op1,KFZ* op2) { if(*op1 < *op2) return true; return false; } //--------------------------------------------------------------------------- #pragma argsused int main(int argc, char* argv[]) { list<KFZ*> data; PKW x("audi","a8",200); LKW y("volvo","x300",400,40); PKW r1("Mercedes","C200",200); PKW r2("Porsche","911",450); //data.push_back(&y); //data.push_back(&r1); data.push_back(&r2); data.push_back(new LKW(y)); data.push_back(&x); //data.push_back(new LKW("Man","irgendwas",400,20)); //data.push_back(new PKW(x)); data.sort<func*>(lower); for(list<KFZ*>::iterator iter=data.begin();iter != data.end();iter++) { (*iter)->Print(); } getchar(); return 0; } //---------------------------------------------------------------------------KFZ.h
//--------------------------------------------------------------------------- #ifndef kfzH #define kfzH #include <string> using std::string; //--------------------------------------------------------------------------- class KFZ { protected: string marke; string model; int ps; string typ; void printRahmen(); public: static int printed; KFZ(){} KFZ(const string& Marke,const string& Model,int Ps); KFZ(const KFZ& op); virtual ~KFZ(){} string Hersteller(); string Model(); string Typ(); int PS(); virtual void Print()=0; KFZ& operator =(const KFZ& op); bool operator <(const KFZ& op); }; //--------------------------------------------------------------------------- class PKW : public KFZ { public: PKW(const string& Marke,const string& Model,int Ps); ~PKW(){} void Print(); }; //--------------------------------------------------------------------------- class LKW : public KFZ { private: int nutzlast; public: LKW(const string& Marke,const string& Model,int Ps,int Nutzlast); LKW(const LKW& op); ~LKW(){} int Nutzlast(); void Print(); LKW& operator =(const LKW& op); }; //--------------------------------------------------------------------------- #endifKFZ.cpp
//--------------------------------------------------------------------------- #pragma hdrstop #include "kfz.h" #include <iostream> #include <conio.h> using namespace std; //--------------------------------------------------------------------------- #pragma package(smart_init) // KFZ int KFZ::printed=0; void printHead() { clrscr(); gotoxy(1,2); cout<<"+------+-----+----------------+--------------------+------+-----------------+"<<endl; cout<<"| Nr | Typ | Hersteller | Modell | PS | Nutzlast |"<<endl; cout<<"+------+-----+----------------+--------------------+------+-----------------+"<<endl; } void KFZ::printRahmen() { gotoxy(1,5+(printed*2)); cout<<"| | | | | | |"<<endl; cout<<"+------+-----+----------------+--------------------+------+-----------------+"<<endl; } //--------------------------------------------------------------------------- KFZ::KFZ(const string& Marke,const string& Model,int Ps): marke(Marke),model(Model),ps(Ps){} //--------------------------------------------------------------------------- string KFZ::Hersteller() { return marke; } //--------------------------------------------------------------------------- string KFZ::Model() { return model; } //--------------------------------------------------------------------------- string KFZ::Typ() { return typ; } //--------------------------------------------------------------------------- int KFZ::PS() { return ps; } //--------------------------------------------------------------------------- KFZ::KFZ(const KFZ& op): marke(op.marke),model(op.model),ps(op.ps),typ(op.typ){} //--------------------------------------------------------------------------- KFZ& KFZ::operator =(const KFZ& op) { this->marke=op.marke; this->model=op.model; this->ps=op.ps; this->typ=op.typ; return *this; } //--------------------------------------------------------------------------- bool KFZ::operator <(const KFZ& op) { string tmp1,tmp2; tmp1=strcat(const_cast<char*>(this->marke.c_str()),const_cast<char*>(this->model.c_str())); tmp2=strcat(const_cast<char*>(op.marke.c_str()),const_cast<char*>(op.model.c_str())); int erg=strcmpi(tmp1.c_str(),tmp2.c_str()); if(erg>0) return false; return true; } //--------------------------------------------------------------------------- //PKW PKW::PKW(const string& Marke,const string& Model,int Ps): KFZ(Marke,Model,Ps) { typ="PKW"; } //--------------------------------------------------------------------------- void PKW::Print() { if(printed==0) printHead(); printRahmen(); int pos = 5 +(printed*2); ++printed; gotoxy(5,pos); cout<<printed; gotoxy(10,pos); cout<<typ; gotoxy(16,pos); cout<<marke; gotoxy(33,pos); cout<<model; gotoxy(54,pos); cout<<ps; gotoxy(61,pos); cout<<" -"; } //--------------------------------------------------------------------------- //LKW LKW::LKW(const string& Marke,const string& Model,int Ps,int Nutzlast): KFZ(Marke,Model,Ps),nutzlast(Nutzlast) { typ="LKW"; } //--------------------------------------------------------------------------- int LKW::Nutzlast() { return nutzlast; } //--------------------------------------------------------------------------- void LKW::Print() { if(printed==0) printHead(); printRahmen(); int pos = 5 +(printed*2); ++printed; gotoxy(5,pos); cout<<printed; gotoxy(10,pos); cout<<typ; gotoxy(16,pos); cout<<marke; gotoxy(33,pos); cout<<model; gotoxy(54,pos); cout<<ps; gotoxy(61,pos); cout<<nutzlast<<" Tonnen"; } //--------------------------------------------------------------------------- LKW& LKW::operator =(const LKW& op) { this->marke=op.marke; this->model=op.model; this->typ=op.typ; this->ps=op.ps; this->nutzlast=op.nutzlast; return *this; } //--------------------------------------------------------------------------- LKW::LKW(const LKW& op) : KFZ(op.marke,op.model,op.ps),nutzlast(op.nutzlast){typ="LKW";} //---------------------------------------------------------------------------danke schonmal
mfg walljumper
-
Da du sowieso virtual benutzt ist dieses Typ-Unterscheiden vollkommen unnötig und auch soweit ich das überblicke Grund des Fehlers. Bentutz einfach (*iter)->Print(). Oder versuch mal Typ() virtual zu machen, aber das ist eigentlich nicht die richtige Vorgehensweise. Dank Polymorphie brauchst du garkeine Typunterscheidung, der Compiler macht das zur Laufzeit für dich.
-
danke für die schneller Antwort
ich hab das mal korrigiert (auch oben in meinem 1. post) es hat sich auch etwas verändert, aber es funktioniert noch immer nicht richtig.
-
Du hängst in operator< einen String an einen konstanten String an und überschreibst damit fremden Speicher. Das resultierende Stück aus konstantem String und Speicher dahinter lässt Du dann lesen und in string umwandeln (Lesen von fremdem Speicher). Das kann nur schiefgehen.
Versuch doch mal das offensichtliche

return (marke + model) < (op.marke + op.model);
-
^^ mit den offensichtlichen dingen hab ich es nicht so. Komm nie auf die einfachsten dinge.
naja funktioniert jetzt. vielen dank euch beiden.
edit: ok geht doch nich so ganz Groß/Kleinschreibung wird noch berücksichtigt aber das müsste ich alleine gelöst bekommen.