Why does VS2010 give syntax errors when syntax is

2020-02-12 17:52发布

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!

2条回答
淡お忘
2楼-- · 2020-02-12 18:41

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.

查看更多
放荡不羁爱自由
3楼-- · 2020-02-12 18:50

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.

查看更多
登录 后发表回答