这是我的标题:
#ifndef TIMING_H
#define TIMING_H
#define MAX_MESSAGES 1000
typedef Message* MessageP; //inside the class?
class Timing {
public:
Timing();
private:
struct Message {
Agent *_agent;
double _val;
};
MessageP* _msgArr;
int _waitingMsgs;
};
我的问题是:我必须将 typedef 放在 MessageP* _msgArr 正上方的类 block 中,还是可以将它放在所有#define 附近?
它不输出编译错误所以我不确定。
最佳答案
在类之外,该类型必须被称为Timing::Message
,所以
typedef Timing::Message* MessageP;
但这只有在Timing::Message
声明之后才有可能,而MessageP
是在Timing
声明完成之前使用的,所以这是不可能的。
此外,该结构是 private:
成员,因此无论如何您都不能在外部定义它。 typedef
必须在 Timing
类中完成。
class Timing {
public:
Timing();
private:
struct Message {
Agent *_agent;
double _val;
};
typedef Message* MessageP; // <--
MessageP* _msgArr;
int _waitingMsgs;
};
您没有收到编译错误可能是因为全局范围内的另一个 Message
类型已经存在。
关于c++ - 在类头中包含 typedef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3653412/