如何修复“之前的预期主表达式”'令牌'错误?(How do I fix a “Expected Primary

系统教程 行业动态 更新时间:2024-06-14 16:57:17
如何修复“之前的预期主表达式”'令牌'错误?(How do I fix a “Expected Primary-expression before ')' token” error?)

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

错误:')'令牌之前的预期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; } }

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

最满意答案

showInventory(player); 将类型作为参数传递。 这是非法的,你需要传递一个对象。

例如,类似于:

player p; showInventory(p);

我猜你有这样的事情:

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

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

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

std::string toDo(player& p) { //.... showInventory(p); //.... }

showInventory(player); is passing a type as parameter. That's illegal, you need to pass an object.

For example, something like:

player p; showInventory(p);

I'm guessing you have something like this:

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

which is awful. First, don't name the object the same as your type. Second, in order for the object to be visible inside the function, you'll need to pass it as parameter:

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

and

std::string toDo(player& p) { //.... showInventory(p); //.... }

更多推荐

本文发布于:2023-04-12 19:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/3a1ee365b3c3210f42e592fb9e60e873.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:令牌   表达式   错误   Primary   Expected

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!