gpt4 book ai didi

c++ - 在类头中包含 typedef

转载 作者:IT老高 更新时间:2023-10-28 22:36:59 25 4
gpt4 key购买 nike

这是我的标题:

#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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com