1

名为Universes的类使用States类型的数据成员,而States使用Universes类型的对象。我正在使用 Visual C++ 2010 Express(如果这有什么不同的话)。

状态.h:

class Universes;

extern Universes universe;

class States
{
public:

    int relations;

    States();
};

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

宇宙.h

#include "States.h"

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

测试.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
4

1 回答 1

0

在您尝试访问universe.state_no时,Universes该类是不完整的(它是前向声明的)。

解决此问题的一种干净方法是将定义States::States移至States.cpp,并确保States.cpp#includes Universes.h

于 2013-03-02T17:26:15.473 回答