Im in the process of creating a bigger program which will allow two players to battle each other, but this will require me passing an array of pointers to objects of the FIGHTER class into a FightFunction function. Ive made a smaller example program to test this. The text I'm using to learn C++ doest actually say how to pass an array into a function so Ive tried passing the pointer to the array into the function, but the program Ive ended up with won't compile. As I'm new to this I'm assuming I've made an obvious mistake, or I'm not understnading it right so any help putting it right is much appreciated. It should be pretty obvious from the code what I want the program to do.
Ive marked with numbers the lines that the compiler is finding errors on. The list of errors at the bottom.
Thanks in advance.
These are the compilier errors:Code:#include <iostream> using namespace std; class FIGHTER { public: FIGHTER() { HP = 50; } ~FIGHTER(); int SetHP(int hp) { HP = hp; } int GetHP() const { return HP; } private: int HP; }; int FightFunction(int*); int main() { int error; FIGHTER *fighter_array[2]; FIGHTER* ptr_a; FIGHTER* ptr_b; ptr_a = new FIGHTER; ptr_b = new FIGHTER; fighter_array[0] = ptr_a; fighter_array[1] = ptr_b; fighter_array[0]->SetHP(200); cout << "\n\nHP of player 1 before function: " << fighter_array[0]->GetHP(); cout << "\nHP of player 2 before function: " << fighter_array[1]->GetHP(); error = FightFunction(int *fight_array) // Line 31 if (error == 0) { cout << "\n\n\n\nHP of player 1 after function: " << fighter_array[0]->GetHP(); cout << "\nHP of player 2 after function: " << fighter_array[1]->GetHP(); } else cout << "\n\n\n\nError"; cin.ignore(); cin.get(); return 0; } int FightFunction(int *fighter_array) { fighter_array[1]->SetHP(300); // Line 48 return 0; }
"test.cpp": E2188 Expression syntax in function main() at line 31
"test.cpp": E2288 Pointer to structure required on left side of -> or ->* in function FightFunction(int *) at line 48
It also warns that the fighter_array is never used in the function.
Thanks again.