Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I fix a "Expected Primary-expression before ')' token" error?

Tags:

c++

token

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

}
like image 968
Tux Avatar asked Oct 13 '12 20:10

Tux


People also ask

What does error expected primary expression before token mean?

This typically means that your code is missing a closing '}'. For every opening '{' there should be a closing '}' expected primary-expression before ')' token. Sometimes this happens when the variable passed into a function isn't the type the function expected.

How do you solve expected primary expression before token in Arduino?

You have to start with the first line and slowly fix every line. Compare your sketch with an example. There is no ';' after a #include, as you can see in a example from the DHT library that you use. Fix the 'define' or the 'const int' for the pin number, use an example to see how it is done.

How do you fix expected unqualified ID before token?

– Adjust the Curly Braces To Fix the Expected Unqualified Id Error. You should match the opening and closing curly braces in your code to ensure the right quantity of brackets. The code should not have an extra or a missing curly bracket.


2 Answers

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);
    //....
}
like image 145
Luchian Grigore Avatar answered Nov 14 '22 20:11

Luchian Grigore


showInventory(player);     // I get the error here.

void showInventory(player& obj) {   // By Johnny :D

this means that player is an datatype and showInventory expect an referance to an variable of type player.

so the correct code will be

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

}
like image 33
Adrian Herea Avatar answered Nov 14 '22 22:11

Adrian Herea