Please help
the attached code is buggy,
It is from an Introductory C++ tutorial,
According to what I have learned,
Delegation is supported in languages like Python and managed C++ dot net .
Some programmers told me that Managed C++ could handle delegation,
Please tell, is this object oriented delegation ?
I use gcc version 3.3.2
best Regards
Morten Gulbrandsen
===
4eList1605_UNIX.cpp: At global scope:
4eList1605_UNIX.cpp:275: error: ISO C++ forbids declaration of `operator+' with no type
4eList1605_UNIX.cpp: In member function `void PartsCatalog::ShowAll()':
4eList1605_UNIX.cpp:276: error:
no matching function for call to `PartsList:: Iterate(<unknown type>)'
4eList1605_UNIX.cpp:210: error: candidates are: void PartsList::Iterate(void (Part::*)() const) const
// Listing 16.5 Delegating to a Contained PartsList #include <iostream> using namespace std; // **************** Part ************ // Abstract base class of parts class Part { public: Part():itsPartNumber(1) {} Part(int PartNumber): itsPartNumber(PartNumber){} virtual ~Part(){} int GetPartNumber() const { return itsPartNumber; } virtual void Display() const =0; private: int itsPartNumber; }; // implementation of pure virtual function so that // derived classes can chain up void Part::Display() const { cout << "\nPart Number: " << itsPartNumber << endl; } // **************** Car Part ************ class CarPart : public Part { public: CarPart():itsModelYear(94){} CarPart(int year, int partNumber); virtual void Display() const { Part::Display(); cout << "Model Year: "; cout << itsModelYear << endl; } private: int itsModelYear; }; CarPart::CarPart(int year, int partNumber): itsModelYear(year), Part(partNumber) {} // **************** AirPlane Part ************ class AirPlanePart : public Part { public: AirPlanePart():itsEngineNumber(1){}; AirPlanePart (int EngineNumber, int PartNumber); virtual void Display() const { Part::Display(); cout << "Engine No.: "; cout << itsEngineNumber << endl; } private: int itsEngineNumber; }; AirPlanePart::AirPlanePart (int EngineNumber, int PartNumber): itsEngineNumber(EngineNumber), Part(PartNumber) {} // **************** Part Node ************ class PartNode { public: PartNode (Part*); ~PartNode(); void SetNext(PartNode * node) { itsNext = node; } PartNode * GetNext() const; Part * GetPart() const; private: Part *itsPart; PartNode * itsNext; }; // PartNode Implementations... PartNode::PartNode(Part* pPart): itsPart(pPart), itsNext(0) {} PartNode::~PartNode() { delete itsPart; itsPart = 0; delete itsNext; itsNext = 0; } // Returns NULL if no next PartNode PartNode * PartNode::GetNext() const { return itsNext; } Part * PartNode::GetPart() const { if (itsPart) return itsPart; else return NULL; //error } // **************** Part List ************ class PartsList { public: PartsList(); ~PartsList(); // needs copy constructor and operator equals! void Iterate(void (Part::*f)()const) const; Part* Find(int & position, int PartNumber) const; Part* GetFirst() const; void Insert(Part *); Part* operator[](int) const; int GetCount() const { return itsCount; } static PartsList& GetGlobalPartsList() { return GlobalPartsList; } private: PartNode * pHead; int itsCount; static PartsList GlobalPartsList; }; PartsList PartsList::GlobalPartsList; PartsList::PartsList(): pHead(0), itsCount(0) {} PartsList::~PartsList() { delete pHead; } Part* PartsList::GetFirst() const { if (pHead) return pHead->GetPart(); else return NULL; // error catch here } Part * PartsList::operator[](int offSet) const { PartNode* pNode = pHead; if (!pHead) return NULL; // error catch here if (offSet > itsCount) return NULL; // error for (int i=0;i<offSet; i++) pNode = pNode->GetNext(); return pNode->GetPart(); } Part* PartsList::Find( int & position, int PartNumber) const { PartNode * pNode = 0; for (pNode = pHead, position = 0; pNode!=NULL; pNode = pNode->GetNext(), position++) { if (pNode->GetPart()->GetPartNumber() == PartNumber) break; } if (pNode == NULL) return NULL; else return pNode->GetPart(); } void PartsList::Iterate(void (Part::*func)()const) const { if (!pHead) return; PartNode* pNode = pHead; do (pNode->GetPart()->*func)(); while (pNode = pNode->GetNext()); } void PartsList::Insert(Part* pPart) { PartNode * pNode = new PartNode(pPart); PartNode * pCurrent = pHead; PartNode * pNext = 0; int New = pPart->GetPartNumber(); int Next = 0; itsCount++; if (!pHead) { pHead = pNode; return; } // if this one is smaller than head // this one is the new head if (pHead->GetPart()->GetPartNumber() > New) { pNode->SetNext(pHead); pHead = pNode; return; } for (;;) { // if there is no next, append this new one if (!pCurrent->GetNext()) { pCurrent->SetNext(pNode); return; } // if this goes after this one and before the next // then insert it here, otherwise get the next pNext = pCurrent->GetNext(); Next = pNext->GetPart()->GetPartNumber(); if (Next > New) { pCurrent->SetNext(pNode); pNode->SetNext(pNext); return; } pCurrent = pNext; } } class PartsCatalog { public: void Insert(Part *); int Exists(int PartNumber); Part * Get(int PartNumber); operator+(const PartsCatalog &); void ShowAll() { thePartsList.Iterate(Part::Display); } private: PartsList thePartsList; }; void PartsCatalog::Insert(Part * newPart) { int partNumber = newPart->GetPartNumber(); int offset; if (!thePartsList.Find(offset, partNumber)) thePartsList.Insert(newPart); else { cout << partNumber << " was the "; switch (offset) { case 0: cout << "first "; break; case 1: cout << "second "; break; case 2: cout << "third "; break; default: cout << offset+1 << "th "; } cout << "entry. Rejected!\n"; } } int PartsCatalog::Exists(int PartNumber) { int offset; thePartsList.Find(offset,PartNumber); return offset; } Part * PartsCatalog::Get(int PartNumber) { int offset; Part * thePart = thePartsList.Find(offset, PartNumber); return thePart; } int main() { PartsCatalog pc; Part * pPart = 0; int PartNumber; int value; int choice; while (1) { cout << "(0)Quit (1)Car (2)Plane: "; cin >> choice; if (!choice) break; cout << "New PartNumber?: "; cin >> PartNumber; if (choice == 1) { cout << "Model Year?: "; cin >> value; pPart = new CarPart(value,PartNumber); } else { cout << "Engine Number?: "; cin >> value; pPart = new AirPlanePart(value,PartNumber); } pc.Insert(pPart); } pc.ShowAll(); return 0; } /* g++ -ansi -pedantic -Wall -o 4eList1605_UNIX.out 4eList1605_UNIX.cpp -L /opt/sfw/gcc-3/lib/ -R /opt/sfw/gcc-3/lib/ -lstdc++ bash-2.05$ g++ -ansi -pedantic -Wall -o 4eList1605_UNIX.out 4eList1605_UNIX.cpp -L /opt/sfw/gcc-3/lib/ -R /opt/sfw/gcc-3/lib/ -lstdc++ 4eList1605_UNIX.cpp: In constructor `CarPart::CarPart(int, int)': 4eList1605_UNIX.cpp:44: warning: `CarPart::itsModelYear' will be initialized after 4eList1605_UNIX.cpp:50: warning: base `Part' 4eList1605_UNIX.cpp: In constructor `AirPlanePart::AirPlanePart(int, int)': 4eList1605_UNIX.cpp:68: warning: `AirPlanePart::itsEngineNumber' will be initialized after 4eList1605_UNIX.cpp:75: warning: base `Part' 4eList1605_UNIX.cpp: In member function `void PartsList::Iterate(void (Part::*)() const) const': 4eList1605_UNIX.cpp:207: warning: suggest parentheses around assignment used as truth value 4eList1605_UNIX.cpp: At global scope: 4eList1605_UNIX.cpp:266: error: ISO C++ forbids declaration of `operator+' with no type 4eList1605_UNIX.cpp: In member function `void PartsCatalog::ShowAll()': 4eList1605_UNIX.cpp:267: error: no matching function for call to `PartsList:: Iterate(<unknown type>)' 4eList1605_UNIX.cpp:201: error: candidates are: void PartsList::Iterate(void (Part::*)() const) const bash-2.05$ */