我正在尝试在 C++ 中使用类 java 枚举的解决方案。
我的问题是我试图将枚举用作另一个类的成员。所以首先我们从熟悉的 Planet 枚举开始:
#ifndef PLANETS_H
#define PLANETS_H
class Planet {
public:
static const double G = 6.67300E-11;
// Enum value DECLARATIONS - they are defined later
static const Planet MERCURY;
static const Planet VENUS;
// ...
private:
double mass; // in kilograms
double radius; // in meters
private:
Planet(double mass, double radius) {
this->mass = mass;
this->radius = radius;
}
public:
double surfaceGravity() {
return G * mass / (radius * radius);
}
};
// Enum value DEFINITIONS
// The initialization occurs in the scope of the class,
// so the private Planet constructor can be used.
const Planet Planet::MERCURY = Planet(3.303e+23, 2.4397e6);
const Planet Planet::VENUS = Planet(4.869e+24, 6.0518e6);
#endif // PLANETS_H
然后我们有一个SolarSystem
接受对象的Planet
对象。
#ifndef SOLARSYSTEM_H
#define SOLARSYSTEM_H
#include "Planets.h"
class SolarSystem {
public:
SolarSystem(int distance, const Planet& planet) {
this->distance = distance;
this->planet = planet;
}
private:
int distance; // in kilometers
Planet planet;
};
#endif // SOLARSYSTEM_H
现在,如果我们尝试编译它,我们会得到以下错误:
SolarSystem.h: In constructor 'SolarSystem::SolarSystem(int, const Planet&)':
SolarSystem.h:7:53: error: no matching function for call to 'Planet::Planet()'
SolarSystem.h:7:53: note: candidates are:
Planets.h:17:5: note: Planet::Planet(double, double)
Planets.h:17:5: note: candidate expects 2 arguments, 0 provided
Planets.h:4:7: note: Planet::Planet(const Planet&)
Planets.h:4:7: note: candidate expects 1 argument, 0 provided
该问题可以通过包含一个空的Planet()
构造函数来解决。
我想知道这是否是最合适的解决方案,或者是否有不涉及空构造函数的解决方案。