なぜ私は作ることができないl->set(new StateB);
のですStateA
か? (この行は下にコメントされています)
それは言います:
main.cpp: In member function ‘virtual void StateA::writeName(Lap*, char*)’:
main.cpp:19:4: error: invalid use of incomplete type ‘struct Lap’
main.cpp:3:7: error: forward declaration of ‘struct Lap’
しかし、私はそれを解決できません:(
#include <stdio.h>
class Lap;
class State;
class StateB;
class StateA;
class State { public:
virtual void writeName(Lap *l, char *str) = 0;
};
class StateB : public State { public:
void writeName(Lap *l, char *str) {
printf("%s B\n", str);
}
};
class StateA : public State { public:
void writeName(Lap *l, char *str) {
printf("%s A\n", str);
//l->set(new StateB);
}
};
class Lap { public:
State *ss;
Lap(){
printf("[Lap]\n");
set(new StateA);
}
void set(State *s){
ss = s;
}
void writeName(char *str){
ss->writeName(this, str);
}
};
int main()
{
printf("\n\n");
Lap lap;
lap.writeName((char*)"Fulano");
lap.writeName((char*)"Fulano");
printf("\n\n");
return 0;
}