Problem bei Vererbung
-
Hallo
Also ich hab ein Problem mit der Erreichbarkeit meiner protected Variable.
Hier mal die Classen
class Shape { protected: Color color; public: Shape(); //Konstruktor virtual ~Shape(); //Destructor virtual void draw() const=0; virtual void move(const Coord&)=0; virtual double area()const = 0; virtual void write(ostream& strm)const=0; void setColor(const Color& color=Black); void setType(figType); figType getType() const; void className()const; private: figType type; }; ostream& operator<< (ostream&, const Shape&); ostream& operator<< (ostream&, const Color&); istream& operator>> (istream&, Shape&);class Point:public Shape{ public: //Standart Konstruktor Point(const Coord& xy=Coord(100,100),const Color& color=Color(0,0,0),POINTSTYLE style=CROSS); Point(const Point&); //void read(istream& strm=cin); void write(ostream& strm=cout)const; virtual void draw() const; virtual void move(const Coord& d); virtual double area()const; private: POINTSTYLE style; //Style Coord xy; //Position }; ostream& operator<< (ostream&, const Point&); ostream& operator<< (ostream&, const Color&);Nun kommen die Funktionen die mir Probleme machen.
[cpp]void Point::write(ostream& strm) const
{
strm << "Point: Pos.:"<< xy << "Color:" << color << "Linestyle:" << style <<endl;
}ostream& operator<< (ostream& strm, const Point& p)
{
p.write(strm);
return strm;
}[/cpp]Wenn ich nun aber color entferne wird alles richtig compiliert. Aber ich brauch die Ausgabe von color.
Warum ist color nicht erreichbar?
-
versuchs mal mit
Shape::colornur ne Idee

mfg maddin
-
Was für Compilefehler bekommst du denn?
Eigentlich sollte es sich problemlos übersetzen lassen, auch _mit_ color.
-
Ne leider geht es nicht mit Shape::color
Die Fehlermeldug ist
g++ SimpleOutputDemo.o Interface.o Shape.o Coord.o Line.o Point.o SimpleOutput.o -L/usr/qt/3/lib -L /usr/X11R6/lib -lXext -lX11 -lm -lqt -o Simple
Point.o: In functionPoint::write(std::basic\_ostream<char, std::char\_traits<char> >&) const': Point.cpp:(.text+0x2d9): undefined reference tooperator<<(std::basic_ostream<char, std::char_traits<char> >&, Color const&)'
collect2: ld returned 1 exit status
make: *** [Simple] Error 1
-
Das ist ein Linkerfehler. Hast du denn den hier
ostream& operator<< (ostream&, const Color&);überhaupt irgendwo definiert?
-
Das Problem ha sich erledigt
class Color { public: // administration functions Color(const uchar r, const uchar g, const uchar b) : red(r), green(g), blue(b) { } Color() { red = green = blue = 0; } // access uchar getRed (void) const { return red; } uchar getBlue (void) const { return blue; } uchar getGreen (void) const { return green; } int operator==(const Color& c) const { return (red == c.red) && (blue == c.blue) && (green == c.green); } int operator!=(const Color& c) const { return !(*this == c); } // implementation void setColor (const uchar r, const uchar g, const uchar b) { red = r; green = g; blue = b; } void setRed (uchar const r) { red = r; } void setBlue (uchar const b) { blue = b; } void setGreen (uchar const g) { green = g; } private: uchar red, green, blue; // Color attributes }; // class ColorDie Class color hat keinen operator << darum hat die augabe nicht funktioniert.
Hab nun so gelöstvoid Point::write(ostream& strm) const { strm << "Point: Pos.:"<< xy << "Color:" << (int)(color.getBlue()) << (int)(color.getRed())<<(int)(color.getGreen()) <<"Linestyle:" << style <<endl; }
-
Zed(gast) schrieb:
Die Class color hat keinen operator << darum hat die augabe nicht funktioniert.
Hab nun so gelöstWarum spendierst du ihr dann keinen Operator << ? Deklariert hast du ihn ja schon:
ostream& operator<<(ostream& strm,const Color& c) { strm<<(int)c.getBlue()<<' '<<(int)c.getRed()<<' '<<(int)c.getGreen(); return strm; }(btw, das 'const' bei den Parametern benötigst du nur, wenn du mit Referenzen oder Pointern arbeitest - bei Wertübergabe ist es unnötig)
-
Da kann ich leider nicht so frei Arbeiten.
Da nimmt es unser Prof ziemlich genau.Nach dem Moto was er sagt ist Gesetz