Pages : 1
#1 Le 19/07/2011, à 13:51
- killwin_159
problème héritage (C++)
Bonjour,
Je ne comprends pas cette erreur ? Si je place le constructeur CGraphic(SDL_Surface *surf, const SDL_Rect& coord) en public ça marche, mais je ne vois pas pourquoi ça marcherait pas en protected ?
Voilà, merci d'avance
graphic.h: In constructor ‘Animation::Animation(int, char*, int, const std::string&, SDL_Surface*, SDL_Rect&)’:
graphic.h:19: error: ‘CGraphic::CGraphic(SDL_Surface*, const SDL_Rect&)’ is protected
class Animation : public CGraphic
{
protected :
char *seq;
int nbr_seq;
int cursor;
int nbr_showbmp;
vector<ShowBMP> showbmp;
public :
Animation(int n, char *sq, int nbr_sq, const string& f, SDL_Surface *surf, SDL_Rect& coord);
void show();
};
Animation::Animation(int n, char *sq, int nbr_sq, const string& file, SDL_Surface *surf, SDL_Rect& coord) : cursor(0) {
CGraphic(surf, coord);
...
}
class CGraphic {
public :
SDL_Rect& properties();
protected :
CGraphic();
CGraphic(SDL_Surface *surf, const SDL_Rect& coord);
virtual void show();
virtual bool eval(SDL_Event &ev);
private :
SDL_Surface *surface;
SDL_Rect& graph_properties;
};
CGraphic::CGraphic(SDL_Surface *surf, const SDL_Rect& coord) : surface(surf), graph_properties(coord) {
graph_properties.w = surface->w;
graph_properties.h = surface->h;
}
SDL_Rect& CGrapgic::properties() {
return graph_properties;
}
Hors ligne
#2 Le 19/07/2011, à 15:28
- omc
Re : problème héritage (C++)
Bonjour,
Un peu bizarre ton constructeur, ce n'est pas plutôt ça que tu veux faire ?
Animation::Animation(int n, char *sq, int nbr_sq, const string& file, SDL_Surface *surf, SDL_Rect& coord) :
CGraphic(surf, coord),
cursor(0) {
...
...
}
La construction de la classe de base devrait se trouver dans la zone d'initialisation des données membres.
Hors ligne
#3 Le 23/07/2011, à 21:48
- Teofifil
Re : problème héritage (C++)
Bonjour,
Dans ta classe Graphic tes constructeurs et tes destructeurs doivent être public car destinés à être accessibles en dehors de ta classe.
En raccourci :
Le domaine private sont les méthodes et les attributs propres à la classe qui ne doivent pas être modifiés ou appelées en dehors de celle-ci.
Le domaine protected gère les méthodes propres aux objets.
Hors ligne
Pages : 1