【问题标题】:How do I fix a "Expected Primary-expression before ')' token" error?如何修复“')' 标记之前的预期主表达式”错误?
【发布时间】:2012-10-04 07:36:40
【问题描述】:

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

错误:')' 标记之前的预期主表达式

有人知道如何解决这个问题吗?

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

}

【问题讨论】:

    标签: c++ token


    【解决方案1】:

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

    例如:

    player p;
    showInventory(p);  
    

    我猜你有这样的东西:

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

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

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

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

    【讨论】:

    • 'player' 是对象名称。我正在尝试传递对象“玩家”。
    • @user1743924 真的吗?它在哪里声明(当然不在您发布的代码中)?事实上,showInventory(player&amp; obj) 这行告诉我们player 是一个类型,而不是一个对象。
    • @user1743924 你把变量命名为和类型一样? 不要。无论如何,该变量是在main 中声明的,而不是在toDo 中声明的。您需要将其作为参数传递。
    • int main(int argc, char *argv[]) { player player;
    【解决方案2】:
    showInventory(player);     // I get the error here.
    
    void showInventory(player& obj) {   // By Johnny :D
    

    这意味着 player 是一个数据类型,而 showInventory 期望引用一个 player 类型的变量。

    所以正确的代码是

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

    【讨论】:

    • 正确的方法当然是没有全局变量。正确的方法是将对象作为参数传递。
    • 完全同意你的观点,但是当你有 C 风格的全局函数时,拥有全局对象并不是那么糟糕。
    • 在 C++ 代码中包含任何 C 风格的东西还不错,对……太糟糕了。
    • 为什么不是全局变量?在游戏中,您不想将玩家编程为全局变量吗?
    • 这并不是在 C++ 中我们有类的原因。你可以有一个带有 getter/setter 的 PlayersManager 或类似的东西来处理玩家。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-13
    • 2017-09-24
    • 2012-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多