如何修复"之前的预期主表达式"'令牌'错误?

Tux*_*Tux 6 c++ token

这是我的代码.我一直收到这个错误:

错误:')'令牌之前的预期primary-expression

任何人有任何想法如何解决这个问题?

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;
}

}
Run Code Online (Sandbox Code Playgroud)

Luc*_*ore 5

showInventory(player);正在传递类型作为参数。那是非法的,您需要传递一个对象。

例如,类似:

player p;
showInventory(p);  
Run Code Online (Sandbox Code Playgroud)

我猜你有这样的事情:

int main()
{
   player player;
   toDo();
}
Run Code Online (Sandbox Code Playgroud)

太可怕了 首先,不要给对象命名与您的类型相同的名称。其次,为了使对象在函数内部可见,您需要将其作为参数传递:

int main()
{
   player p;
   toDo(p);
}
Run Code Online (Sandbox Code Playgroud)

std::string toDo(player& p) 
{
    //....
    showInventory(p);
    //....
}
Run Code Online (Sandbox Code Playgroud)