我無法使用MinGW C++將涉及嵌套類的對象模型放在一起。下面是我的公開問題的示例:爲什麼我無法從外部定義嵌套類?
了foo.h:
/*
* foo.h
*
* Created on: Sep 25, 2011
* Author: AutoBot
*/
#ifndef FOO_H_
#define FOO_H_
class Foo
{
public:
class Bar;
Bar bar;
} extern foo;
#endif /* FOO_H_ */
bar.h:
/*
* bar.h
*
* Created on: Sep 25, 2011
* Author: AutoBot
*/
#ifndef BAR_H_
#define BAR_H_
#include <iostream>
using namespace std;
#include "foo.h"
class Foo::Bar
{
public:
void Test() {cout <<"Test!";}
};
#endif /* BAR_H_ */
main.cpp中:
/*
* main.cpp
*
* Created on: Sep 25, 2011
* Author: AutoBot
*/
#include "foo.h"
Foo foo;
int main (int argc, char *argv[])
{
foo.bar.Test();
return 0;
}
Eclipse CDT的構建日誌:
**** Build of configuration Debug for project NestedClassTest ****
**** Internal Builder is used for build ****
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o src\main.o ..\src\main.cpp
In file included from ..\src\main.cpp:10:0:
..\src\/foo.h:15:6: error: field 'bar' has incomplete type
..\src\main.cpp: In function 'int main(int, char**)':
..\src\main.cpp:16:6: error: 'class Foo' has no member named 'bar'
Build error occurred, build is stopped
Time consumed: 171 ms.
所以基本上它不能識別bar.h中bar的定義。這是正常的嗎?爲什麼我不能以這種方式定義嵌套類?這只是MinGW工具鏈的限制嗎?任何幫助/建議表示讚賞。注意:在我的實際程序中,我打算將它的「foo」作爲理論上的singleton類。它將代表應用程序作爲一個整體,並且它的子系統(或「酒吧」)將在類中定義和實例化。我這樣做是爲了讓代碼更易於管理。如果有人有一個更可行的設計模式,請告訴我!
爲什麼你需要一個代表應用程序的對象?該應用程序由_running_這個事實來表示。如果代碼可以執行,那麼你就在應用程序中。我可以理解使用單例提供對某些特定數據(例如文件系統)的訪問權限,但使用單例表示應用程序沒有多大意義。 –
是的,也許我太過分了。我希望單身人員確實管理的一件事是所有子系統的啓動/關閉和服務(通過'start','end'和'update'函數)。這讓我傾向於把所有實際的子系統都放在裏面,但是我可能不應該這樣做,因爲限制代碼的使用。 – AutoBotAM