为什么VS2010语法正确却给出语法错误?
我在使用 VS2010(和 VS2008)时遇到问题,它给了我一个很棒的语法错误列表。然而,语法确实是正确的。这是一个小例子;
我在 .h 文件中有以下代码块,
// 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 是一个类型定义的结构,仅供参考。所有这些函数声明似乎都是正确的语法。然而,在尝试构建时,我从第一个 bool 函数开始,一直到列表中出现以下语法错误。
错误 2 错误 C2059:语法错误:';'
我看不出问题出在哪里。再次强调,这只是一个小例子。我还收到如下语法错误
bool found;
错误 29 错误 C2065:“bool”:未声明的标识符
我真的对这个一无所知。这里发布的代码不是我自己的,它来自数据结构书籍,但它看起来又是正确的。任何帮助将不胜感激。谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
bool
不是 C 中的基本类型。VisualC++ 仅实现 C90,它没有
bool
类型。 C99 通过
标头添加了对bool
的支持,但 Visual C++ 不支持此功能。您应该使用
int
或为bool
创建您自己的 typedef。bool
is not a fundamental type in C.Visual C++ only implements C90, which has no
bool
type. C99 added support forbool
via the<stdbool.h>
header, but Visual C++ does not support this.You should either use
int
or create your own typedef forbool
.检查文件的文件扩展名(包括该标头)。
如果您不告诉 Visual Studio 做任何不同的事情(在项目设置中),Visual Studio 会自动将 .c 文件编译为 C 而不是 C++。
Visual Studio 的“C”支持...很有趣 - 据我了解,它实际上是 C89 而不是 C99,而且您不能只是轻按一下开关即可获得 C99。除了 C89/C99 之外,bool 不是 C 中的内置类型。
您可以将所有文件重命名为 .cpp 以将它们编译为 C++,或者修改项目设置以强制将每个 .c/.cpp/.cc 文件编译为 C++在项目中。
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.