Class 相互使用的类(位于不同的头中)';s对象

Class 相互使用的类(位于不同的头中)';s对象,class,header,Class,Header,名为universe的类使用States类型的数据成员,而States使用universe类型的对象。我使用Visual C++ 2010 Express(如果这有任何区别)。 国家h: class Universes; extern Universes universe; class States { public: int relations; States(); }; States::States() { relations = universe.stat

名为universe的类使用States类型的数据成员,而States使用universe类型的对象。我使用Visual C++ 2010 Express(如果这有任何区别)。 国家h:

class Universes;

extern Universes universe;

class States
{
public:

    int relations;

    States();
};

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

#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;
}

在尝试访问
universe.state\u no
时,
Universes
类不完整(它是向前声明的)

解决此问题的一个干净方法是将
状态::状态的定义移动到
状态.cpp
,并确保
状态.cpp
#包括
Universes.h

可能的重复
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