[英]C++ syntax error and missing type specifier
Visual C++ 2008 gives me this weird error, so I stripped out all of the excess stuff in my header file (util.h ), and it boils down to this: Visual C ++ 2008给了我这个奇怪的错误,所以我删除了头文件(util.h)中所有多余的东西,并将其归结为:
#ifndef UTIL_H
#define UTIL_H
void pause();
#endif
When I try to compile the above code I get this: 当我尝试编译上面的代码时,我得到了:
Compiling...
util.cpp
util.h(4) : error C2144: syntax error : 'void' should be preceded by ';'
util.h(4) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
<snip>
I am deeply confused. 我很困惑。 Even if I comment out the ifndef, define, and endif, preprocessor directives, It still says the same thing. 即使我注释掉了ifndef,define和endif预处理程序指令,它仍然会说同样的话。
After hours of looking, I finally figured it out! 经过数小时的寻找,我终于明白了!
The error is not in the header file itself. 该错误不在头文件本身中。 Header files are copied and pasted into source files, where they are included. 头文件被复制并粘贴到源文件中(在其中包含它们)。 So I looked where I included the file, and I saw in util.cpp: 因此,我查看了包含文件的位置,并在util.cpp中看到了:
#include "stdafx.h"b
#include "util.h"
<snip>
That extra 'b'
is causing the error. 多余的'b'
会导致错误。 C++ thinks it is a variable, but doesn't see a type specifier, so it assumes 'b'
is a new integer. C ++认为它是一个变量,但是没有看到类型说明符,因此它假定'b'
是一个新的整数。 Then it thinks I should terminate its declaration with a ';'
然后它认为我应该以';'
终止其声明';'
to precede 'void'
. 在'void'
之前。 The entire issue is just a stupid extra character, and a not very helpful error message. 整个问题只是一个愚蠢的多余字符,并且不是非常有用的错误消息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.