. Advertisement .
..3..
. Advertisement .
..4..
Hi everyone, I’m learning about cpp. While working, I try undefined. As a result, I get the message:
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;
}
}
error: expected primary-expression before ')' token
What can I do about the “expected primary expression before token c++” issue? Is there a better approach?
The cause:
After looking over your program, I found that the function
showInventory(player);
is passing into a parameter, but it is invalid. Therefore, the error happens.Solution:
You must pass into an object, not a parameter. Let’s see the following example:
It has something like as:
How terrible it is! Remember not to use the same name as your type for the object and you need to give the object a parameter so that it can be seen inside the function.
And
If you follow my above suggestions, you will solve your error successfully.
This means that player is a datatype, and showInventory expects a referance to a variable of type player.
So the code is correct