我在这里有一个类的头规范:
#ifndef FIXEDWINGAIRCRAFT_H
#define FIXEDWINGAIRCRAFT_H
#include <iostream>
class FixedWingAircraft
{
private:
struct Airframe
{
double weight;
};
struct Engine
{
double weight;
double fuel;
};
struct Radio
{
bool state;
double weight;
};
struct Pilot
{
int proficiency;
double weight;
};
public:
void setAirframe(double w)
{
Airframe.weight = w;
}
void setEngine(double w, double f)
{
Engine.weight = w;
Engine.fuel = f;
}
void setRadio(bool s, double w)
{
Radio.state = s;
Radio.weight = w;
}
void setPilot(int p, double w)
{
Pilot.proficiency = p;
Pilot.weight = w;
}
};
#endif
Run Code Online (Sandbox Code Playgroud)
但是当我尝试编译时,我遇到了大量的语法错误:
error C2143: syntax error : missing ';' before '.'
Run Code Online (Sandbox Code Playgroud)
我假设这些是指setter函数,但我不明白为什么会导致问题.我错过了什么?
Luc*_*ore 12
Airframe.weight = w;而所有类似的人都是非法的.Airframe是一个类,而不是一个对象.您可能希望将该类型的对象作为成员并设置其属性.
你可以替换
struct Airframe
{
double weight;
};
Run Code Online (Sandbox Code Playgroud)
同
struct Airframe
{
double weight;
} airframe;
Run Code Online (Sandbox Code Playgroud)
这将为您提供FixedWingAircraft可以访问的类型的成员airframe.