为什么我无法在 StateA
中制作 l-gt;set(新状态B);
? (此行在下面评论)
上面写道:
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
", str);
}
};
class StateA : public State { public:
void writeName(Lap *l, char *str) {
printf("%s A
", str);
//l->set(new StateB);
}
};
class Lap { public:
State *ss;
Lap(){
printf("[Lap]
");
set(new StateA);
}
void set(State *s){
ss = s;
}
void writeName(char *str){
ss->writeName(this, str);
}
};
int main()
{
printf("
");
Lap lap;
lap.writeName((char*)"Fulano");
lap.writeName((char*)"Fulano");
printf("
");
return 0;
}