【发布时间】:2017-04-11 08:50:28
【问题描述】:
我想在我的 C++ 类中使用命名空间,但在编译步骤中遇到了多个问题。
这是我非常简单的类及其头文件。
Class1.h
#if !defined(CLASS1)
#define CLASS1
namespace Test1
{
class Test2::Class2;
class Class1
{
public:
Class1();
virtual ~Class1();
Test2::Class2 *getClass2();
};
}
#endif // !defined(CLASS1)
Class1.cpp
#include "Class1.h"
#include "Class2.h"
using namespace Test1;
using namespace Test2;
Class1::Class1(){
}
Class1::~Class1(){
}
Class2 *Class1::getClass2(){
return new Class2();
}
Class2.h
#if !defined(CLASS2)
#define CLASS2
namespace Test2
{
class Test1::Class1;
class Class2
{
public:
Class2();
virtual ~Class2();
Test1::Class1 *getClass1();
};
}
#endif // !defined(CLASS2)
Class2.cpp
#include "Class2.h"
#include "Class1.h"
using namespace Test1;
using namespace Test2;
Class2::Class2(){
}
Class2::~Class2(){
}
Class1 *Class2::getClass1(){
return new Class1();
}
如果我不使用命名空间,我可以编译类而不会出现任何错误。但如果我尝试使用命名空间,则会出现以下错误:
1>------ Rebuild All started: Project: TestsIncludes, Configuration: Debug Win32 ------
1>TestsIncludes.cpp
1>stdafx.cpp
1>Class2.cpp
1>c:\users\stefv\test\class2.h(6): error C2653: 'Test1': is not a class or namespace name
1>c:\users\stefv\test\class2.h(6): error C2079: 'Class1' uses undefined class 'Test2::Test1'
1>c:\users\stefv\test\class2.h(13): error C2027: use of undefined type 'Test2::Test1'
1>c:\users\stefv\test\class2.h(6): note: see declaration of 'Test2::Test1'
1>c:\users\stefv\test\class2.h(13): error C2143: syntax error: missing ';' before '*'
1>c:\users\stefv\test\class2.h(13): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\stefv\test\class2.h(13): error C2238: unexpected token(s) preceding ';'
1>c:\users\stefv\test\class2.cpp(13): error C2872: 'Class1': ambiguous symbol
1>c:\users\stefv\test\class2.h(6): note: could be 'int Test2::Class1'
1>c:\users\stefv\test\class1.h(9): note: or 'Test1::Class1'
1>c:\users\stefv\test\class2.cpp(13): error C2143: syntax error: missing ';' before '*'
1>c:\users\stefv\test\class2.cpp(13): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\stefv\test\class2.cpp(13): error C2039: 'getClass1': is not a member of 'Test2::Class2'
1>c:\users\stefv\test\class2.h(9): note: see declaration of 'Test2::Class2'
1>c:\users\stefv\test\class2.cpp(13): error C2059: syntax error: '{'
1>c:\users\stefv\test\class2.cpp(13): error C2143: syntax error: missing ';' before '{'
1>c:\users\stefv\test\class2.cpp(13): error C2447: '{': missing function header (old-style formal list?)
1>Class1.cpp
1>c:\users\stefv\test\class1.h(6): error C2653: 'Test2': is not a class or namespace name
1>c:\users\stefv\test\class1.h(6): error C2079: 'Class2' uses undefined class 'Test1::Test2'
1>c:\users\stefv\test\class1.h(13): error C2027: use of undefined type 'Test1::Test2'
1>c:\users\stefv\test\class1.h(6): note: see declaration of 'Test1::Test2'
1>c:\users\stefv\test\class1.h(13): error C2143: syntax error: missing ';' before '*'
1>c:\users\stefv\test\class1.h(13): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\stefv\test\class1.h(13): error C2238: unexpected token(s) preceding ';'
1>c:\users\stefv\test\class1.cpp(13): error C2872: 'Class2': ambiguous symbol
1>c:\users\stefv\test\class1.h(6): note: could be 'int Test1::Class2'
1>c:\users\stefv\test\class2.h(9): note: or 'Test2::Class2'
1>c:\users\stefv\test\class1.cpp(13): error C2143: syntax error: missing ';' before '*'
1>c:\users\stefv\test\class1.cpp(13): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\stefv\test\class1.cpp(13): error C2039: 'getClass2': is not a member of 'Test1::Class1'
1>c:\users\stefv\test\class1.h(9): note: see declaration of 'Test1::Class1'
1>c:\users\stefv\test\class1.cpp(13): error C2059: syntax error: '{'
1>c:\users\stefv\test\class1.cpp(13): error C2143: syntax error: missing ';' before '{'
1>c:\users\stefv\test\class1.cpp(13): error C2447: '{': missing function header (old-style formal list?)
1>Generating Code...
1>Done building project "TestsIncludes.vcxproj" -- FAILED.
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
我在命名空间或转发类方面的错误在哪里?
感谢您的帮助!
【问题讨论】:
-
Fwd 在
Test1命名空间外声明namespace Test2 { class Class2; },反之亦然? -
@virgesmith 您不能在
class Test2::Class2;等命名空间中转发声明类。您必须在命名空间内转发声明它。
标签: c++ namespaces