I'm trying to implement a turn based game for the course project. The game has a 'status' base class and from which derived the actual statuses such as 'poisoned', 'paralyzed' or 'choke'(which is the test case that goes wrong).
A status need to be setup first before it is inflicted to a character object by a move object, and the SetupStatus()
function varies between different derived classes of the status class.
The problem is, when I am trying to call the SetupStatus()
function of the 'choke' class (choke::SetupStatus()
), which is derived from the base class 'status', it always call the function status::SetupStatus()
. The function is called in the cmove::LaunchMove(...)
.
Where did I do wrong?
I missed the virtual declaration in the first place. I added it, but it didn't solve the problem.
main.cpp
int main() {
cmove aqua_ball = AquaBall();
field TestField;
character Irrawa = IRRAWA();
character Mew = MEW();
aqua_ball.LaunchMove(&Irrawa, &Mew, &TestField);
}
cmove.cpp
void cmove::LaunchMove(character *speller, character *taker, field *thisField) {
//some other codes...
if (slf_adStat.size() != 0) {
for (int i = 0; i < slf_adStat.size(); i++) {
status tempStatus = slf_adStat[i];
cout << tempStatus.get_information() << endl; //Output tempStatus info
tempStatus.SetupStatus(speller, taker, thisField);//Where the function call goes wrong
cout << tempStatus.get_information() << endl; //Output tempStatus info
(*speller).add_status(tempStatus);
}
}
//some other codes...
};
status.h
class status{
public:
string sta_name, sta_info;
//some other codes...
virtual void SetupStatus(character* selfCharacter, character* oppoCharacter, field* currentField);
string get_information();
//some other codes...
};
class choke : public status{
public:
choke(){
sta_name = "CHOKE";
sta_info = "Reduce speed by 15. Last 3 turns.";
//some codes...
}
virtual void SetupStatus(character* selfCharacter, character* oppoCharacter, field* currentField);
//some codes...
};
status.cpp
void status::SetupStatus(character* selfCharacter, character* oppoCharacter, field* currentField){
cout << "General status setup..."<< endl;
}
string status::get_information(){
string output = "[" + sta_name + "]" + ":\n" + sta_info + "\nLEFT:" + to_string(nT) + "turns\n";
return output;
}
void choke::SetupStatus(character* selfCharacter, character* oppoCharacter, field* currentField){
sta_ds = -15;
cout << sta_ds << "Choke status setup..." << endl;
}
Output:
[CHOKE]:
Reduce speed by 15. Last 3 turns.
LEFT:3turns
General status setup...
[CHOKE]:
Reduce speed by 15. Last 3 turns.
LEFT:3turns
Since the tempStatus objects before and after the SetupStatus() function are all printed as expected, the tempStatus is a choke class object, this is not a problem related to object slicing.
All the code can be find here if necessary: https://github.com/Irrawa/godsFighting