2013-03-02 24 views
1

稱爲宇宙類使用類型國的數據成員,而美國使用類型宇宙的對象。我正在使用Visual C++ 2010 Express(如果這有什麼區別)。類(位於不同的報頭),該使用彼此的對象

States.h:

class Universes; 

extern Universes universe; 

class States 
{ 
public: 

    int relations; 

    States(); 
}; 

States::States() 
{ 
    relations = universe.state_no; 
} 

Universes.h

#include "States.h" 

class Universes 
{ 
public: 
    States state; 
    int state_no; 
}; 

Test.cpp的

#include "stdafx.h" 
#include <iostream> 
#include <conio.h> 

#include "Universes.h" 

using namespace System; 

int main(array<System::String ^> ^args) 
{ 
    Universes universe; 
    _getch(); 
    return 0; 
} 

我不斷收到以下錯誤:

States.h(16): error C2027: use of undefined type 'Universes' 
States.h(1) : see declaration of 'Universes' 
States.h(16): error C2228: left of '.state_no' must have class/struct/union 
+0

[Header files inclusion/Forward declaration]的可能重複(http://stackoverflow.com/questions/2832714/header-files-inclusion-forward-declaration) – SomeWittyUsername 2013-03-02 17:27:06

回答

0

在嘗試訪問universe.state_no時,Universes類不完整(它是前向聲明的)。

解決此問題的一個簡單方法是將States::States的定義移動到States.cpp,並確保States.cpp #includes Universes.h