我正在用C++编写一些东西。我有我要包含一个到另一个在如下因素2类(这些只是头文件):尝试在另一个类中使用类时出错
//Timing.h
#ifndef _Timing_h
#define _Timing_h
#include "Agent.h"
class Timing{
private:
typedef struct Message{
Agent* _agent; //i get here a compilation problem
double _id;
} Message;
typedef struct MessageArr{
} MessageArr;
public:
Timing();
~Timing();
};
#endif
//Agent.h
#ifndef _Agent_h
#define _Agent_h
#include <string>
#include "Timing.h"
using namespace std;
class Agent{
public:
Agent(string agentName);
void SetNextAgent(Agent* nextAgent);
Agent* GetNextAgent();
void SendMessage(Agent* toAgent, double id);
void RecieveMessage(double val);
~Agent();
private:
string _agentName;
double _pID;
double _mID;
Agent* _nextAgent;
};
#endif
的编译错误是在结构的定义里面的Timing.h文件:
预期“;” '*'令牌之前
我在做什么错?
是啊!它确实有帮助, 但是如果我想在Agent.h文件中创建一些Timing.h函数呢? 我需要在Timing.h中包含Agent.h吗? – 2010-09-01 12:53:11
你能解释为什么这解决了这个问题吗? – 2010-09-01 12:54:51
因为当你所做的全部工作是使用一个类作为返回类型,一个指针或引用成员,或一个指针或引用参数时,编译器不需要查看该类的完整定义以便继续。一个简单的前瞻性声明说“有一个具有以下名称的类”就足够了。如果您想实际*使用*给定类型的对象,则需要进行完整的分解。因此,在time.cpp中,实际使用'Message :: _ agent'字段*时,您确实需要包含'Agent'(agent.h)的完整定义。 – 2010-09-01 13:01:48