我正在玩结构体和类,我看到了我想尝试的一个非常酷的编码:x-macro。
我的代码分为 3 位,标题、x 宏和主 cpp 文件。该程序尚未完成,还有代码覆盖和抛光要做,但我正在尝试使用 x-macro 构建一个结构,然后我想将结构的内容打印到屏幕上。
这是我的 x 宏
#define X_AIRCRAFT \
X(int, Crew) \
X(int, SeatingCapacity) \
X(int, Payload) \
X(int, Range) \
X(int, TopSpeed) \
X(int, CargoCapacity) \
X(int, FuelCapacity) \
X(int, Engines) \
X(int, Altitude) \
X(double, mach) \
X(double, Wingspan)
这是我的标题(现在很贫瘠)
#include <iostream>
#include <string>
#ifndef X_AIRCRAFT
#include "xmacro.xmacro"
#endif // !
using namespace std;
typedef struct {
#define X(type, name) type name;
X_AIRCRAFT
#undef X
}Public_Airplane;
//Prototypes
void iterater(Public_Airplane *p_a);
这是我的main()
(我在这里剪掉了一堆代码。总而言之,我在这里所做的是构建一个具有不同属性的 Airplane 类。然后我构建了三个不同的子类,它们继承了 Airplane 的属性并做了自己的事情。所以除非你们认为我的问题出在那儿,否则我将完全避免发布课程。我要做的只是发布无法正常工作的功能...)
#include <iostream>
#include <string>
#include <iomanip>
#include "aircraft.h"
#ifndef X_AIRCRAFT
#include "xmacro.xmacro"
#endif // !
using namespace std;
int main()
{
Public_Airplane p_a;
iterater(&p_a);
system("pause");
return 0;
}
void iterater(Public_Airplane *p_a)
{
//I want to print to screen the contents of my x-macro (and therefore my struct)
#define X(type, name) cout << "Value: = " << name;
X_AIRCRAFT
#undef X
}
我以前从未使用过宏,这就是我现在尝试这样做的原因。但据我了解,预处理后的代码应如下所示:
int crew;
int SeatingCapacity;
int Payload
int Range;
int TopSpeed;
int CargoCapacity;
int FuelCapacity;
int Engines;
int Altitude;
double mach;
double Wingspan;
cout << "Value: = " << Crew; (and so on down the list).
我做错了什么让我无法获得上面的代码输出?