[英]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;
}
}
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); // I get the error here.
void showInventory(player& obj) { // By Johnny :D
這意味着播放器是一種數據類型,而showInventory期望對播放器類型的變量進行referance。
所以正確的代碼將是
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++;
}
}
players myPlayers[10];
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(myPlayers[0]); // or any other index,also is not necessary to have an array
return NULL;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.