karlakir | Salut,
j'ai une erreur (vous l'aurez deviné) en lien avec d'apres ce que j'ai lu, les destructeurs virtuelles.
pour resumer les codes:
j'ai une fonction:
- point
- graphical object qui est une classe abstraite qui include point
- differentes figures classe publique de graphical object (ex class cercle: public graphicalobject)
- une classe group qui contient 50 obj graph
avant que j'ajoute la classe group, tout marchait bien
depuis, j'ai une erreur vtable
voici les differents codes:
point.cpp
Code :
- #include "Point.hpp"
- Point Point::ORIGINE(0,0);
- Point::~Point() {}
- double Point::dist(Point& a, Point& b)
- {
- return sqrt((a._x-b._x)*(a._x-b._x)+(a._y-b._y)*(a._y-b._y));
- }
- std::string Point::tostring()
- {
- std::string chaine("un point:\n" );
- std::ostringstream ossx;
- std::ostringstream ossy;
- ossx << _x;
- ossy << _y;
- chaine += "x = "+ossx.str()+"\n"+"y = "+ossy.str()+"\n";
-
- return chaine;
- }
- void Point::display()
- {
- std::cout << this->tostring() << std::endl;
- }
|
point.hpp
Code :
- #include <iostream>
- #include <math.h>
- #include <sstream>
- class Point
- {
- private:
- int _x;
- int _y;
- public:
- static Point ORIGINE;
- Point():_x(0),_y(0) {};
- Point(int inx, int iny): _x(inx),_y(iny) {};
- ~Point();
- int getx() const {return _x;}
- int gety() const {return _y;}
- void setPoint(int inx, int iny) {_x=inx,_y=iny;}
- void setx(int inx) {_x=inx;}
- void sety(int iny) {_y=iny;}
- static double dist(Point& a, Point& b);
- std::string tostring();
- void display();
- };
|
GraphicalObject.hpp
Code :
- #ifndef GraphicalObject_HPP
- #define GraphicalObject_HPP
- #include <iostream>
- #include "Point.hpp"
- typedef enum COLORS
- {
- rouge,
- vert,
- bleu
- }COLORS;
- class GraphicalObject
- {
- private:
- static int _cpt_id; //compteur
- int _id;
- Point _origin;
- int _width;
- int _height;
- COLORS _couleurs;
-
- public:
- GraphicalObject();
- GraphicalObject(Point inp, int inwidth, int inheight);
- virtual ~GraphicalObject() {_cpt_id--;};
-
- void setorigin(Point inp) {_origin=inp;}
- void setwidth(int inwidth) {_width=inwidth;}
- void setheight(int inheight) {_height=inheight;}
- void setcolor(COLORS incoul) {_couleurs=incoul;}
-
- int getid() {return _id;};
- Point getorigin() {return _origin;};
- int getwidth() {return _width;};
- int getheight() {return _height;};
- COLORS getcolors() {return _couleurs;};
-
- virtual std::string tostring() = 0;
- virtual void display() = 0;
- };
- #endif
|
GraphicalObject.cpp
Code :
- #include "GraphicalObject.hpp"
- int GraphicalObject::_cpt_id = 0;
- GraphicalObject::GraphicalObject()
- {
- _cpt_id +=1;
-
- _id = _cpt_id;
- _origin=Point::ORIGINE;
- _width = 0;
- _height = 0;
- _couleurs = vert;
- }
- GraphicalObject::GraphicalObject(Point inp, int inwidth, int inheight)
- {
- _cpt_id +=1;
-
- _id = _cpt_id;
- _origin = inp;
- _width = inwidth;
- _height = inheight;
- _couleurs = vert;
- }
|
Cercle.hpp
Code :
- #ifndef Cercle_HPP
- #define Cercle_HPP
- #include "GraphicalObject.hpp"
- class Cercle: public GraphicalObject
- {
- public:
- void display();
- std::string tostring();
- };
- #endif
|
Cercle.cpp
Code :
- #include "Cercle.hpp"
- std::string Cercle::tostring()
- {
- std::string chaine("cercle:\n" );
- std::ostringstream ossx;
- std::ostringstream ossy;
- std::ostringstream ossh;
- std::ostringstream ossid;
-
- ossx << getorigin().getx();
- ossy << getorigin().gety();
- ossh << getheight();
- ossid << getid();
- chaine += "id: "+ossid.str()+"\n"+"x: "+ossx.str()+"\n"+"y: "+ossy.str()+"\n";
- chaine += "rayon: "+ossh.str()+"\n";
-
- return chaine;
- }
- void Cercle::display()
- {
- std::cout << this->tostring() << std::endl;
- }
|
Group.hpp
Code :
- #ifndef Group_HPP
- #define Group_HPP
- #include "GraphicalObject.hpp"
- class Group: public GraphicalObject
- {
-
- private:
- GraphicalObject * _list[50];
- int _compt;
-
- public:
- Group();
- virtual ~Group() {};
-
- bool attach(GraphicalObject *pOG);
- void detach(GraphicalObject *pOG);
-
- void display();
- std::string tostring();
-
- };
- #endif
|
Group.cpp
Code :
- #include "Group.hpp"
- Group::Group()
- {
- for(int i=0;i<50;i++) _list[i]=0;
- _compt = 0;
- }
- bool Group::attach(GraphicalObject * pOG)
- {
- int res =0;
- if(_compt<50)
- {
- int i=0;
- while(_list[i]!=0) i++;
- _list[i] = pOG;
- _compt++;
-
- Point temp(std::min(getorigin().getx(), pOG->getorigin().getx()),
- std::min(getorigin().gety(), pOG->getorigin().gety()));
- setorigin(temp);
-
- setwidth(std::max(getwidth(), getorigin().getx()+pOG->getwidth()));
- setheight(std::max(getheight(), getorigin().gety()+pOG->getheight()));
-
- res=1;
- }
- return res;
- }
- void Group::detach(GraphicalObject * pOG)
- { }
|
Erreur
Code :
- g++ -Wall -ansi -pedantic -Wextra Cercle.o GraphicalObject.o Group.o line.o main.o Point.o Rectangle.o -lm -o ./exe
- Group.o: In function `Group::Group()':
- Group.cpp:(.text+0x16): undefined reference to `vtable for Group'
- Group.o: In function `Group::Group()':
- Group.cpp:(.text+0x68): undefined reference to `vtable for Group'
- main.o: In function `Group::~Group()':
- main.cpp:(.text._ZN5GroupD1Ev[Group::~Group()]+0xb): undefined reference to `vtable for Group'
- collect2: ld returned 1 exit status
- make: *** [pMed] Erreur 1
|
voila, (et sinon, le code doit pas etre génial, je debute )
merci d'avance Message édité par karlakir le 15-11-2010 à 21:08:36 ---------------
Si vous avez rien à faire, cliquez ici:
|