I am trying to use a solution for java-like enumerations in C ++ .
My problem is that I am trying to use enum as a member in another class. So, first we start with the familiar enumeration of the Planet:
#ifndef PLANETS_H
#define PLANETS_H
class Planet {
public:
static const double G = 6.67300E-11;
static const Planet MERCURY;
static const Planet VENUS;
private:
double mass;
double radius;
private:
Planet(double mass, double radius) {
this->mass = mass;
this->radius = radius;
}
public:
double surfaceGravity() {
return G * mass / (radius * radius);
}
};
const Planet Planet::MERCURY = Planet(3.303e+23, 2.4397e6);
const Planet Planet::VENUS = Planet(4.869e+24, 6.0518e6);
#endif
Then we have an object SolarSystemthat accepts objects 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;
Planet planet;
};
#endif
Now, if we try to compile this, we get the following errors:
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
The problem can be fixed by including an empty constructor Planet().
I was wondering if this is the most appropriate fix or if there is a solution that does not include an empty constructor.
source
share