[英]Namespaces and forward classes in C++
我想在我的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 ==========
我在命名空間或轉發類上的錯誤在哪里?
謝謝您的幫助 !
您的問題在於轉發聲明類的方式。 代替:
namespace Test1
{
class Test2::Class2;
}
您應該這樣聲明:
namespace Test2
{
class Class2;
}
在名稱空間Test1之外。 當您聲明class Test2::Class2
您是在告訴編譯器您在類Test2
有一個名為Class2
類,這顯然是不一樣的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.