Hilfe und Erklärung eines Buchbeispiels
-
Hallo liebes Forum,
ich wollte mich etwas der "operator-überladung" widmen, und wollte mal ein Buchbeispiel ausprobieren, hier mal der gesamte Quelltext:
koord.hpp:#ifndef KOORD_HPP #define KOORD_HPP #include <iostream> class Koord { private: int x; int y; public: Koord() : x(0), y(0) { } Koord(int newx, int newy) : x(newx), y(newy) { } Koord operator + (const Koord&) const; Koord operator - () const; void operator += (const Koord&); void printOn (ostream&) const; }; inline Koord Koord::operator + (const Koord& p) const { return Koord(x+p.x, y+p.y); } inline Koord Koord::operator - () const { return Koord(-x, -y); } inline void Koord::operator+=(const Koord& p) { x += p.x; y += p.y; } inline void Koord::printOn (ostream& strm) const { strm << '(' << x << ',' << y << ')'; } inline ostream& operator<< (ostream& strm, const Koord& p) { p.printOn(strm); return strm; } #endif
geoobj.hpp:
#ifndef GEOOBJ_HPP #define GEOOBJ_HPP #include "koord.hpp" class GeoObj { protected: Koord refpunkt; GeoObj(const Koord& p) : refpunkt(p) { } public: virtual void move(const Koord& offset) { refpunkt += offset; } virtual void draw() const = 0; virtual ~GeoObj() { } }; #endif
linie.hpp:
#ifndef LINIE_HPP #define LINIE_HPP #include <iostream> #include "geoobj.hpp" class Linie : public GeoObj { protected: Koord p2; public: Linie(const Koord& a, const Koord& b) : GeoObj(a), p2(b) { } virtual void draw() const; virtual void move(const Koord&); virtual ~Linie() { } }; void Linie::draw() const { std::cout << "Linie von " << refpunkt << " bis " << p2 << std::endl; } inline void Linie::move(const Koord& offset) { refpunkt += offset; p2 += offset; } #endif
kreis.hpp:
#ifndef KREIS_HPP #define KREIS_HPP #include <iostream> #include "geoobj.hpp" class Kreis : public GeoObj { protected: unsigned radius; public: Kreis (const Koord& m, unsigned r) : GeoObj(m), radius(r) { } virtual void draw() const; virtual ~Kreis() { } }; inline void Kreis::draw() const { std::cout << "Kreis um Mittelpunkt " << refpunkt << " mit Radius " << radius << std::endl; } #endif
untitled.cpp:
#include "linie.hpp" #include "kreis.hpp" #include "geoobj.hpp" void geoObjAusgeben(const GeoObj&); int main(int argc, char* argv[]) { Linie l1(Koord(1,2), Koord(3,4)); Linie l2(Koord(7,7), Koord(0,0)); Kreis k(Koord(3,3), 11); GeoObj* menge[10]; menge[0] = &l1; menge[1] = &k; menge[2] = &l2; for(int i=0; i<3; i++) { menge[i]->draw(); menge[i]->move(Koord(3, -3)); } geoObjAusgeben(l1); geoObjAusgeben(k); geoObjAusgeben(l2); return 0; } void geoObjAusgeben(const GeoObj& obj) { obj.draw(); }
Nun das folgende Problem was ich nun habe ist, das ich folgende Compilermeldungen bekommen, und ich nicht weiß warum, könnte mich da bitte jemand aufklären?
"koord.hpp": E2293 ) erwartet in Zeile 19 "koord.hpp": E2147 Parameterdeklaration darf nicht mit 'ostream' beginnen in Zeile 35 "koord.hpp": E2316 'Koord::printOn(int &) const' ist kein Element von 'Koord' in Zeile 35 "koord.hpp": E2092 Speicherklasse 'inline' ist hier nicht erlaubt in Zeile 39 "koord.hpp": E2141 Fehler in der Deklarationssyntax in Zeile 39 "linie.hpp": E2303 Typname erwartet in Zeile 7 "linie.hpp": E2312 'GeoObj' ist keine eindeutige Basisklasse von 'Linie' in Funktion Linie::Linie(const Koord &,const Koord &) in Zeile 12 "linie.hpp": E2451 Undefiniertes Symbol 'refpunkt' in Funktion Linie::move(const Koord &) in Zeile 25 "kreis.hpp": E2303 Typname erwartet in Zeile 7 "kreis.hpp": E2312 'GeoObj' ist keine eindeutige Basisklasse von 'Kreis' in Funktion Kreis::Kreis(const Koord &,unsigned int) in Zeile 12 "untitled.cpp": E2293 ) erwartet in Zeile 5 "untitled.cpp": E2451 Undefiniertes Symbol 'GeoObj' in Funktion main(int,char * *) in Zeile 12 "untitled.cpp": E2451 Undefiniertes Symbol 'menge' in Funktion main(int,char * *) in Zeile 12 "untitled.cpp": E2268 Aufruf der undefinierten Funktion 'geoObjAusgeben' in Funktion main(int,char * *) in Zeile 23 "untitled.cpp": E2188 Ausdruckssyntax in Funktion main(int,char * *) in Zeile 25 "untitled.cpp": E2293 ) erwartet in Zeile 29 *** 16 Fehler bei der Compilierung *** BCC32 beendet mit Fehler-Code: 1 Erzeugen wegen Fehler abgebrochen
Vielen Dank im Voraus für Eure Hilfe
MfG Roman1311
-
Hallo,
du solltest mal versuchen, den Namespace mit anzugeben:
void printOn (std::ostream&) const;
-
Peinlich *ganzrotwerd*
Das wars, einfach nur "std::" vor ostream& setzen.Jedoch hätte ich noch eine andere Frage, und zwar wieso muss ich alle operator in der Klasse deklarieren, nur nicht:
inline ostream& operator<< (ostream& strm, const Koord& p) { p.printOn(strm); return strm; }
Danke noch vielmals
MfG Roman1311
-
Weil das Buch mies ist.
opeator+ und - sollten non-member sein.op<< darf kein member sein, weil sonst this 'links' stehen würde:
obj<<cout;
wäre wohl n bisschen ungutsiehe auch
http://tutorial.schornboeck.net/operatoren_ueberladung.htm und http://tutorial.schornboeck.net/operatoren_ueberladung2.htm
-
opeator+ und - sollten non-member sein.
Quatsch. + ja, aber - doch nicht!
-
oh, ich hab irgendwie nur an den binaeren op- gedacht. der unaere muss natuerlich member sein.