依赖项、命名空间和标头
我相信自己给自己挖了一个坑。在将我的单个文件、几千行代码块分解成多个文件时,我似乎严重搞乱了程序的结构。
我是 C++ 及其头文件和命名空间管理的新手,所以我一直在学习。 我想我不完全理解 #include、using 和命名空间如何相互关联以及什么传输到其他文件等。
通过阅读 MSDN 文档,我可以看到我的问题的一些细节,但解决方案却让我困惑。 截至目前,我有四个 .cpp 文件和标头:
1) 主文件
2) GameData .cpp 及其包含在命名空间 pData 中的标头
3) GameSettings .cpp 及其包含在命名空间 pSettings
4 中的标头) GeneralScreens .cpp 及其包含在命名空间 pScreens 中的标头
编译时,调试器会出现 100 多个错误,例如:
>c:\program files (x86)\microsoft visual studio 10.0\vc\include\utility(454): error C2447: '{' : missing function header (old-style formal list?)
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\utility(463): error C2039: 'pair' : is not a member of 'std'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\utility(463): error C2955: 'pScreens::std::pair' : use of class template requires template argument list
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\utility(173) : see declaration of 'pScreens::std::pair'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cwchar(33): error C2873: 'wcsrtombs' : symbol cannot be used in a using-declaration
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cwchar(41): error C2039: 'wctob' : is not a member of '`global namespace''
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cwchar(41): error C2873: 'wctob' : symbol cannot be used in a using-declaration
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cwchar(42): error C2039: 'wmemchr' : is not a member of '`global namespace''
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cwchar(42): error C2873: 'wmemchr' : symbol cannot be used in a using-declaration
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cwchar(42): error C2039: 'wmemcmp' : is not a member of '`global namespace''
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cwchar(42): error C2873: 'wmemcmp' : symbol cannot be used in a using-declaration
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cwchar(42): error C2039: 'wmemcpy' : is not a member of '`global namespace''
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cwchar(42): error C2873: 'wmemcpy' : symbol cannot be used in a using-declaration
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cwchar(43): error C2039: 'wmemmove' : is not a member of '`global namespace''
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\new(93): error C2039: 'nothrow_t' : is not a member of 'std'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\new(93): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\new(93): error C2143: syntax error : missing ',' before '&'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\new(99): error C2039: 'new_handler' : is not a member of 'std'
这些错误是否表明某些内容?
我假设我引用 std 库的方式有问题,尽管我不知道如何查明错误,因为所有错误都在库文件中。
I believe have dug myself into a hole. While breaking up my single file, couple thousand line chunk of code into multiple files I appear to have grossly screwed up the structure of the program.
I'm new to C++ and its header files and namespace management so I've been learning as I go.
I guess I don't fully understand how #include, using, and namespaces interrelate and what transfers over to other files, etc.
Reading through MSDN documentation I can see bits and pieces of my problem but the solution eludes me.
As of now I have four .cpp files and headers:
1) A Main File
2) A GameData .cpp and its header that is contained in the namespace pData
3) A GameSettings .cpp and its header that is contained in the namespace pSettings
4) A GeneralScreens .cpp and its header that is contained in the namespace pScreens
Upon compile the debugger spews over 100 of error like:
>c:\program files (x86)\microsoft visual studio 10.0\vc\include\utility(454): error C2447: '{' : missing function header (old-style formal list?)
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\utility(463): error C2039: 'pair' : is not a member of 'std'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\utility(463): error C2955: 'pScreens::std::pair' : use of class template requires template argument list
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\utility(173) : see declaration of 'pScreens::std::pair'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cwchar(33): error C2873: 'wcsrtombs' : symbol cannot be used in a using-declaration
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cwchar(41): error C2039: 'wctob' : is not a member of '`global namespace''
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cwchar(41): error C2873: 'wctob' : symbol cannot be used in a using-declaration
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cwchar(42): error C2039: 'wmemchr' : is not a member of '`global namespace''
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cwchar(42): error C2873: 'wmemchr' : symbol cannot be used in a using-declaration
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cwchar(42): error C2039: 'wmemcmp' : is not a member of '`global namespace''
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cwchar(42): error C2873: 'wmemcmp' : symbol cannot be used in a using-declaration
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cwchar(42): error C2039: 'wmemcpy' : is not a member of '`global namespace''
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cwchar(42): error C2873: 'wmemcpy' : symbol cannot be used in a using-declaration
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cwchar(43): error C2039: 'wmemmove' : is not a member of '`global namespace''
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\new(93): error C2039: 'nothrow_t' : is not a member of 'std'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\new(93): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\new(93): error C2143: syntax error : missing ',' before '&'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\new(99): error C2039: 'new_handler' : is not a member of 'std'
Are these errors indicative of something?
I'm assuming something is wrong with the way I'm referencing the std library though I have no idea how to pinpoint the error because all the errors are in library files.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果头文件中存在类或结构定义,则很可能在末尾缺少分号。然后,编译器将它看到的每个标识符视为该类的实例,而不是您所期望的。然后编译器会抱怨,因为它不知道如何处理它看到的杂散 {}。
检查实现文件中
之前#include
的标头;该标头很可能是故障所在。旁注:为避免此问题,请始终在 .CPP 文件中将
#include
标准标头放在您自己的标头之前。例如,不这样做
,您将不会得到像这样埋在标准头中的编译错误。
Most likely you're missing a semicolon on the end if a class or struct definition in a header file. The compiler then treats every identifier it sees as an instance of that class instead of what you're expecting. Then the compiler complains because it has no idea what to do with the stray {}s it sees.
Check the header that's
#include
d just before<utility>
in your implementation file; that header is most likely where the fault lies.Side note: To avoid this problem, always
#include
standard headers before your own headers in a .CPP file. For example, instead ofdo
That way you won't get compile errors like this buried in the standard headers.
第一个编译器错误将是最好的线索:
我猜这里发生的是您可能有一个没有结束分号的类或结构声明。所以你有这样的事情:
但总的来说,薪酬接近&特别注意第一个编译器错误。
正如 @Billy 在下面的评论中提到的, struct 和 class 的规则是相同的。无论哪种情况,您都需要分号。此外,老式 C 代码通常会像这样键入定义结构:
这里,您仍然需要分号。
The first compiler error is going to be your best clue:
I'm guessing that what's happening here is you may have a class or struct declaration with no ending semicolon. So you have something like this:
But overall, pay close & special attention to the first compiler error.
As @Billy mentions in the comments below, the rules for
struct
andclass
are the same. In either case, you need the semicolon. Also, old-school C code will often typedef structs like this:Here, you still need the semicolon.
如果使用 Visual C++,打开第一个 cpp 文件并按 CTRL+F7,这将仅编译 cpp 文件,检查第一个错误并尝试更正此错误。修复后,如果错误仍然存在,请转到下一个 cpp 文件。
If using Visual C++, open the first cpp file and press CTRL+F7, this will compile only the cpp file, check the first error and try to correct this. When fixed go to the next cpp file if the error persists.
比利的尝试是一次不错的尝试,但通常的反应是缺少“;”是由编译器这么说的。另一方面,缺少“}”可能会严重混淆随后包含的标准标头,因为它们的所有内容都在应该关闭的名称空间或类的范围内。还, ';'缺少可以更立即检测到,而缺少“}”不一定是错误,因此编译器只是对在该范围内没有意义的第一件事感到不满。
此外,2010 编译器的实用程序中的第 454 行是 std::pair 的 tuple_size 特化的左大括号。后置预处理器的定义如下所示:
如果您忘记了关闭命名空间的“}”,那么该命名空间就不再存在
pair<>
了。相反,因为它是这样声明的:无论什么范围没有关闭,现在都是声明 std 和 struct tuple_size
的地方。 ::std::pair<...>; >
对解析器没有任何意义。因为它不是一个有效的名称,所以它往往会假装它不存在,然后在全局范围内使用 '{' ,在它变得非常令人困惑之前没有任何意义。顺便说一句,制作导致问题的最少示例的一个经常不言而喻的好处是,您最终会发现问题并试图使其发生。
Billy's attempt is a decent one, but the usual response to a missing ';' is for the compiler to say so. A missing '}' on the other hand can seriously confuse standard headers included afterward because all their content is within the scope of either the namespace or class that should have been closed. Also, ';' missing is more immediately detectable while a missing '}' isn't necessarily an error so the compiler just pisses a fit at the first thing that doesn't make sense in that scope.
Furthermore, at line 454 in utility for the 2010 compiler is the opening brace for a specialization of tuple_size for std::pair. Post preprocessor that definition would look like so:
If you have forgotten a '}' closing a namespace then that's not where
pair<>
exists anymore. Instead, since it's declared like so:Whatever scope wasn't closed is now where
std
is being declared andstruct tuple_size< ::std::pair<...> >
doesn't make any sense to the parser. Since it's not a valid name it tends to pretend it's not even there and then '{' at the global scope without anything of sense before it is pretty f'n confusing.BTW, an often unspoken benefit of making minimal examples that cause the problem is that you end up FINDING the problem trying to make it happen.