Rhys van der Waerden
Rhys van der Waerden

Reputation: 3847

Getting bizarre "expected primary-expression" error

I'm getting a really strange error when making a method call:

   /* input.cpp */

#include <ncurses/ncurses.h>
#include "input.h"
#include "command.h"

Input::Input ()
{
    raw ();
    noecho ();
}

Command Input::next ()
{
    char input = getch ();
    Command nextCommand;

    switch (input)
    {
    case 'h':
        nextCommand.setAction (ACTION_MOVELEFT);
        break;
    case 'j':
        nextCommand.setAction (ACTION_MOVEDOWN);
        break;
    case 'k':
        nextCommand.setAction (ACTION_MOVEUP);
        break;
    case 'l':
        nextCommand.setAction (ACTION_MOVERIGHT);
        break;
    case 'y':
        nextCommand.setAction (ACTION_MOVEUPLEFT);
        break;
    case 'u':
        nextCommand.setAction (ACTION_MOVEUPRIGHT);
        break;
    case 'n':
        nextCommand.setAction (ACTION_MOVEDOWNLEFT);
        break;
    case 'm':
        nextCommand.setAction (ACTION_MOVEDOWNRIGHT);
        break;
    case '.':
        nextCommand.setAction (ACTION_WAIT);
        break;
    }


    return nextCommand;
}

and the error:

Administrator@RHYS ~/code/rogue2
$ make
g++ -c -Wall -pedantic -g3 -O0 input.cpp
input.cpp: In member function `Command Input::next()':
input.cpp:21: error: expected primary-expression before '=' token
input.cpp:24: error: expected primary-expression before '=' token
input.cpp:27: error: expected primary-expression before '=' token
input.cpp:30: error: expected primary-expression before '=' token
input.cpp:33: error: expected primary-expression before '=' token
input.cpp:36: error: expected primary-expression before '=' token
input.cpp:39: error: expected primary-expression before '=' token
input.cpp:42: error: expected primary-expression before '=' token
input.cpp:45: error: expected primary-expression before '=' token
make: *** [input.o] Error 1

Sorry about the lack of linenumbers, the errors occur on the lines "nextCommand.setAction(...)", which is totally bizarre considering that they don't contain a '='.

Any ideas? Thanks,

Rhys

Upvotes: 0

Views: 3345

Answers (2)

James McNellis
James McNellis

Reputation: 355069

Here's the only thing I can think of (without seeing more code) that would cause this:

Your identifiers in all-caps are macros, defined something like this:

#define ACTION_MOVELEFT = 1
#define ACTION_MOVEDOWN = 2

and so on. When the macros are then expanded, you end up with code like:

case 'h':
    nextCommand.setAction (= 1);
    break;

The = is not used to define a macro; for object-like macros, everything following the macro name until the newline ending the macro definition is part of the replacement list. So, the macros should be defined as follows:

#define ACTION_MOVELEFT 1
#define ACTION_MOVEDOWN 2

and so on.

However, you should consider using an enumeration to enforce type safety and to avoid using the preprocessor when it doesn't need to be used:

enum ActionType
{
    ActionMoveLeft,
    ActionMoveDown
};

Or, at the very least, use const ints rather than #defines.

Upvotes: 5

paxdiablo
paxdiablo

Reputation: 881513

Generally, if you're sure the errors are on the lines you think they're on, and the character being complained about doesn't exist, you should look to see what the preprocessor is expanding those lines into, such as by using the gcc -E flag to see the output of said preprocessor.

I suspect ACTION_MOVELEFT and its brethren may be expanding to something unexpected, but only viewing the preprocessor output will tell you for sure.

Upvotes: 2

Related Questions