How to fix the error "Expected primary expression before") "token"?

Here is my code. I keep getting this error:

error: expected primary expression before ')' token

Anyone have any ideas how to fix this?

void showInventory(player& obj) { // By Johnny :D for(int i = 0; i < 20; i++) { std::cout << "\nINVENTORY:\n" + obj.getItem(i); i++; std::cout << "\t\t\t" + obj.getItem(i) + "\n"; i++; } } std::string toDo() //BY KEATON { std::string commands[5] = // This is the valid list of commands. {"help", "inv"}; std::string ans; std::cout << "\nWhat do you wish to do?\n>> "; std::cin >> ans; if(ans == commands[0]) { helpMenu(); return NULL; } else if(ans == commands[1]) { showInventory(player); // I get the error here. return NULL; } } 
+4
source share
2 answers

showInventory(player); passes the type as a parameter. This is illegal, you need to pass the object.

For example, something like:

 player p; showInventory(p); 

I assume you have something like this:

 int main() { player player; toDo(); } 

which is terrible. First, do not name the object the same as your type. Secondly, in order for the object to be visible inside the function, you need to pass it as a parameter:

 int main() { player p; toDo(p); } 

and

 std::string toDo(player& p) { //.... showInventory(p); //.... } 
+4
source
 showInventory(player); // I get the error here. void showInventory(player& obj) { // By Johnny :D 

this means that the player is a data type, and showInventory is waiting for a reference to a player type variable.

so the correct code will be

  void showInventory(player& obj) { // By Johnny :D for(int i = 0; i < 20; i++) { std::cout << "\nINVENTORY:\n" + obj.getItem(i); i++; std::cout << "\t\t\t" + obj.getItem(i) + "\n"; i++; } } players myPlayers[10]; std::string toDo() //BY KEATON { std::string commands[5] = // This is the valid list of commands. {"help", "inv"}; std::string ans; std::cout << "\nWhat do you wish to do?\n>> "; std::cin >> ans; if(ans == commands[0]) { helpMenu(); return NULL; } else if(ans == commands[1]) { showInventory(myPlayers[0]); // or any other index,also is not necessary to have an array return NULL; } } 
0
source

Source: https://habr.com/ru/post/1439576/


All Articles