我想在我的C ++类中使用名称空间,但在编译步骤中我遇到了多个问题。
以下是我的简单类及其头文件。
#if !defined(CLASS1)
#define CLASS1
namespace Test1
{
class Test2::Class2;
class Class1
{
public:
Class1();
virtual ~Class1();
Test2::Class2 *getClass2();
};
}
#endif // !defined(CLASS1)
#include "Class1.h"
#include "Class2.h"
using namespace Test1;
using namespace Test2;
Class1::Class1(){
}
Class1::~Class1(){
}
Class2 *Class1::getClass2(){
return new Class2();
}
#if !defined(CLASS2)
#define CLASS2
namespace Test2
{
class Test1::Class1;
class Class2
{
public:
Class2();
virtual ~Class2();
Test1::Class1 *getClass1();
};
}
#endif // !defined(CLASS2)
#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 ==========
我在命名空间或转发类中的错误在哪里?
感谢您的帮助!
答案 0 :(得分:2)
你的问题在于前进的方式 - 声明你的课程。而不是:
namespace Test1
{
class Test2::Class2;
}
你应该这样声明:
namespace Test2
{
class Class2;
}
命名空间Test1之外。当您声明class Test2::Class2
时,您告诉编译器您在类Class2
中有一个名为Test2
的类,显然是不一样的。