2010-04-19 60 views
0

制作方法打电话时我发现了一个很奇怪的错误:获得离奇的“预期主表达式”错误

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

和错误:

[email protected] ~/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 

对不起缺乏linenumbers的,错误发生在“nextCommand.setAction(...)”行上,考虑到它们不包含'=',这完全是奇怪的。

任何想法? 谢谢,

里斯

+4

全部大小的宏中是否存在标识符,如果是,它们是什么扩展? – 2010-04-19 01:26:42

回答

5

这是我能想到的唯一的事情(没有看到更多的代码),将导致此:

你标识符中的所有大写的宏定义是这样的:

#define ACTION_MOVELEFT = 1 
#define ACTION_MOVEDOWN = 2 

等等。当宏然后扩大,你最终像代码:

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

=不用于定义一个宏;对于类宏对象,宏名后面的所有内容直到结束宏定义的换行符都是替换列表的一部分。所以宏应该定义如下:

#define ACTION_MOVELEFT 1 
#define ACTION_MOVEDOWN 2 

等等。

然而,你应该考虑使用一个枚举执行类型安全,避免使用预处理时,它不需要使用:

enum ActionType 
{ 
    ActionMoveLeft, 
    ActionMoveDown 
}; 

或者,至少是,使用const int做得相当比#define s。

+1

+1,可能就是这样。当然,使用const int而不是定义会更聪明,因为它们也会携带类型信息(编译器对象而不是预处理对象)。 – paxdiablo 2010-04-19 01:37:58

+2

@paxdiablo:同意。甚至更好,枚举。 – 2010-04-19 01:44:54

+0

谢谢,这是完全正确的。愚蠢的错误... – 2010-04-19 01:47:47

2

一般来说,如果您确定错误出现在您认为自己正在使用的行上,并且被投诉的字符不存在,您应该查看预处理程序将这些行扩展到的内容,例如如通过使用gcc -E标志来查看所述预处理器的输出。

我怀疑ACTION_MOVELEFT和它的兄弟可能会扩展到意想不到的地方,但只有查看预处理器输出才能确定地告诉你。

相关问题