这是我的代码。我不断收到此错误:
error: expected primary-expression before ')' token
有人知道如何解决这个问题吗?
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;
}
}
最佳答案
showInventory(player);
正在传递一个类型作为参数。这是非法的,您需要传递一个对象。
例如,像这样的东西:
player p;
showInventory(p);
我猜你有这样的事情:
int main()
{
player player;
toDo();
}
这太糟糕了。首先,不要将对象命名为与您的类型相同的名称。其次,为了使对象在函数内部可见,您需要将其作为参数传递:
int main()
{
player p;
toDo(p);
}
和
std::string toDo(player& p)
{
//....
showInventory(p);
//....
}
关于c++ - 如何修复 "Expected Primary-expression before ' )' token"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12876822/