Hoe los ik een fout ‘Verwachte primaire expressie vóór ‘)’ token’ op?

Hier is mijn code. Ik krijg steeds deze foutmelding:

fout: verwachte primaire expressie vóór token ‘)’

Iemand enig idee hoe dit op te lossen?

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

Antwoord 1, autoriteit 100%

showInventory(player);geeft een type door als parameter. Dat is illegaal, je moet een object doorgeven.

Bijvoorbeeld zoiets als:

player p;
showInventory(p);  

Ik vermoed dat je zoiets als dit hebt:

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

wat verschrikkelijk is. Ten eerste, noem het object niet dezelfde naam als uw type. Ten tweede, om het object zichtbaar te maken in de functie, moet je het doorgeven als parameter:

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

en

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

Antwoord 2, Autoriteit 14%

showInventory(player);     // I get the error here.
void showInventory(player& obj) {   // By Johnny :D

Dit betekent dat de speler een datatype en showinventorie is, verwacht een verwijzingen naar een variabele van het type speler.

Dus de juiste code is

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

Other episodes