Why does VS2010 give syntax errors when syntax is correct?

和自甴很熟 提交于 2019-11-28 05:26:16

问题


I am having a problem with VS2010 (and VS2008) giving my a great list of syntax errors. However, the syntax is indeed correct. Here is a small example;

I have the following code block inside a .h file

// Prototype Declarations
LIST*   createList     (int (*compare) (void*, void*));
LIST*   destroyList    (LIST* plist);
int     addNode      (LIST* pList, void* dataInPtr);
bool    removeNode      (LIST* pList, void* keyPtr, void** dataOutPtr);
bool    searchList      (LIST* pList, void* pArgu, void** pDataOut);
bool    retrieveNode    (LIST* pList, void* pArgu, void** dataOutPtr);
bool    traverse        (LIST* pList, int fromWhere, void** dataOutPtr);
int     listCount    (LIST* pList);
bool    isListEmpty     (LIST* pList);
bool    isListFull      (LIST* pList);

LIST is a typedef'd struct, FYI. All of these function declarations appear to be correct syntax. Yet, when attempting to build, I get the following syntax errors starting at the first bool function, going down the list.

Error 2 error C2059: syntax error : ';'

I'm failing to see where the problem lies. Again, this is just a small example. I also receive syntax errors such as the following

bool found;

Error 29 error C2065: 'bool' : undeclared identifier

I'm truly at a lost on this one. The code posted here isn't my own, it's from a data structures book, but again it looks correct. Any help would be appreciated. Thanks!


回答1:


bool is not a fundamental type in C.

Visual C++ only implements C90, which has no bool type. C99 added support for bool via the <stdbool.h> header, but Visual C++ does not support this.

You should either use int or create your own typedef for bool.




回答2:


Check the file extension of the file including that header.

Visual Studio will automatically compile .c files as C rather than C++ if you don't tell it to do any differently (in the project settings).

Visual Studio's "C" support is... interesting - to my understanding it is in fact C89 rather than C99, and you can't just flick a switch to get C99. C89/C99 aside, bool is not a builtin type in C.

You can rename all your files to .cpp to compile them as C++, or modify the project settings to force compilation as C++ for every .c/.cpp/.cc file in the project.



来源:https://stackoverflow.com/questions/5671312/why-does-vs2010-give-syntax-errors-when-syntax-is-correct

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!