依赖项,命名空间和标题

时间:2010-12-15 19:41:08

标签: c++ namespaces reference header-files std

我相信已经把自己挖进一个洞。在分解我的单个文件时,将几千行代码分成多个文件,我似乎已经严重搞砸了程序的结构。
我是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库的方式有问题但我不知道如何查明错误,因为所有错误都在库文件中。

4 个答案:

答案 0 :(得分:3)

如果头文件中有类或结构定义,则最有可能在末尾错过分号。然后,编译器将它看到的每个标识符视为该类的实例,而不是您期望的那样。然后编译器抱怨,因为它不知道如何处理它看到的迷路{}。

检查实施文件中#include之前的<utility> d标题;那个标题最有可能是故障所在。

附注:为避免此问题,请始终在.CPP文件中自己的标头之前添加#include个标准标头。例如,而不是

// MyGameThing.cpp
#include "MyGameThing.hpp"
#include <utility>
#include <string>

// code

DO

// MyGameThing.cpp
#include <utility>
#include <string>
#include "MyGameThing.hpp"

// code

这样你就不会得到像这样的编译错误。

答案 1 :(得分:3)

第一个编译错误将是您最好的线索:

  

c:\ program files(x86)\ microsoft visual studio 10.0 \ vc \ include \ utility(454):错误C2447:'{':缺少函数头(旧式正式列表?)

我猜这里发生的事情是你可能有一个没有结尾分号的类或结构声明。所以你有这样的事情:

struct Foo
{
}  // <---  no semicolon here will generate funky errors

但总的来说,付出近距离&amp;特别注意第一个编译错误。

正如@Billy在下面的评论中提到的,structclass的规则是相同的。在任何一种情况下,您都需要分号。此外,老式的C代码通常会像这样输入结构:

typedef struct tag_Foo
{
} Foo; // <-- still need the semicolon

在这里,需要分号。

答案 2 :(得分:0)

如果使用Visual C ++,打开第一个cpp文件并按CTRL + F7,这将只编译cpp文件,检查第一个错误并尝试更正此错误。修复后,如果错误仍然存​​在,请转到下一个cpp文件。

答案 3 :(得分:0)

比利的尝试是一个不错的尝试,但通常对失踪的回应';'是编译器这么说的。另一方面,缺少'}'会严重混淆之后包含的标准头文件,因为它们的所有内容都在应该关闭的命名空间或类的范围内。还有,';'缺失更容易被检测到,而缺少的'}'不一定是错误,所以编译器只是在第一件事情中适合那个在该范围内没有意义的东西。

此外,在2010年编译器的实用程序的第454行是std :: pair的tuple_size特化的开头大括号。发布预处理器的定义如下:

template < ... >
struct tuple_size< ::std::pair<_Ty1, _Ty2> >

如果您忘记了'}'关闭命名空间,那么pair<>就不再存在了。相反,因为它被声明如此:

namespace std { template < ... > struct pair ... }

无论什么范围未关闭,现在宣布std并且struct tuple_size< ::std::pair<...> >对解析器没有任何意义。由于它不是一个有效的名称,它往往会假装它甚至不存在,然后“{”在全球范围内没有任何意义,然后才会让人感到困惑。

顺便说一下,制作导致问题的最小例子的一个经常不言而喻的好处是,你最终找到试图实现它的问题。