c++ - 如何修复 "Expected Primary-expression before ' )' token"错误?

标签 c++ token

这是我的代码。我不断收到此错误:

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/

相关文章:

c++ - 在 Linux ARM 上使用 libusb 的状态热敏打印机

C++ 单元测试 : Stubs (not mocks)?

c++ - 是否有一个 vector 可以处理非标准位长的整数?

c++ - 为什么我的代码与这些s表达式不匹配正则表达式?

c - 为什么在使用 Bison 的 C 语法分析期间无法识别已定义的标记?

android - Oauth 2.0 刷新 token 过期

c++ - ofstream 由多个线程共享 - 一段时间后崩溃

c++ - 如何使用 std::sqrt 作为 std::function?

python - 如何让 Flask_Security 中的 auth_token_required 工作?

token - JWT 和服务器端 token 存储