c++ class inheritance, undefined reference to 'Class::Constructor()'
Solution 1
You think because you are not calling SystemBody()
you don't need to define it. However you are calling it indirectly.
And do't do
SystemBody() {};
as suggested. This is not what you want. Instead remove it completely if you don't use it.
Your class Star inherits from SystemBody
. That means when a new Star
is constructed the constructor for SystemBody
is called.
This line
Star(int systembodyindex, int starsystemindex)
{
is actually compiled to
Star(int systembodyindex, int starsystemindex) :
SystemBody() // Here
{
The compiler calls the default constructor for SystemBody
if you don't call one yourself.
If you think about it you need to initialize SystemBody
somehow when you create a new Star
. You can do this explicitly like
Star(int systembodyindex, int starsystemindex) :
SystemBody(systembodyindex) // Here
{
Solution 2
you defined many default constructors but didn't implement them. Instead of
StarSystem(); // <- it is OK if you implement this somewhere but you didn't
write
StarSystem(){}
^^ this is empty implementation
phimath
Updated on April 16, 2020Comments
-
phimath about 4 years
Edit: Posted fixed code at the bottom. Thanks everyone for your help!
I am just learning c++ and am having trouble with inheritance. I have searched and searched and tried anything I can but I can not get this code to compile while retaining the functionality that I want it to have.
I feel like I am making a stupid mistake or maybe I'm just missing some big concept, but if anyone could take a look at it I'd really appreciate it!
If I comment out the the 3 lines in the StarSystem Constructor that create objects, then it compiles, so I know this has to do with the issue.
#include <iostream> #include <vector> #include <string> #include <stdlib.h> #include <time.h> using namespace std; class SystemBody { public: SystemBody(); int systembodyindex; int starsystemindex; SystemBody(int systembodyindex, int starsystemindex) { cout << "StarSystem " << starsystemindex << ": creating empty SystemBody " << systembodyindex << endl; } }; class Star : public SystemBody { public: Star(); string startype; Star(int systembodyindex, int starsystemindex) { cout << "StarSystem " << starsystemindex << ": converting empty SystemBody into Star " << systembodyindex << endl; } }; class Planet : public SystemBody { public: Planet(); string planettype; Planet(int systembodyindex, int starsystemindex) { cout << "StarSystem " << starsystemindex << ": converting empty SystemBody into Planet " << systembodyindex << endl; } }; class ExitNode : public SystemBody { public: ExitNode(); vector<int> connectedindexlist; ExitNode(int systembodyindex, int starsystemindex) { cout << "StarSystem " << starsystemindex << ": converting empty SystemBody into Exit Node " << systembodyindex << endl; } }; class StarSystem { public: StarSystem(); int starsystemindex; vector<StarSystem> connectedlist; vector<Planet> planetlist; StarSystem(int index) { starsystemindex = index; cout << "--Creating StarSystem: " << starsystemindex << endl; int numberofbodies = (rand() % 4) + 2; for ( int i = 0; i < numberofbodies; i +=1 ) { if ( i == 0 ) { Star body(i, starsystemindex); } else if ( i == numberofbodies ) { ExitNode body(i, starsystemindex); } else { Planet body(i, starsystemindex); } } } void addConnection(StarSystem connectedstarsystem) { cout << "--StarSystem " << starsystemindex << ": Adding connection to StarSystem " << connectedstarsystem.starsystemindex << endl; connectedlist.push_back(connectedstarsystem); } }; int main() { srand(time(0)); StarSystem starsystem0(0); return 0; }
EDIT:
thanks to everyone for your help! just posting the fixed code here in case any one in the future might find this useful.
#include <iostream> #include <vector> #include <string> #include <stdlib.h> #include <time.h> using namespace std; class SystemBody { public: int systembodyindex; int starsystemindex; SystemBody ( ) { cout << "----SystemBody BEING CREATED WITH NO PARAMETERS" << endl; } SystemBody ( int bodyindex, int systemindex ) { systembodyindex = bodyindex; starsystemindex = systemindex; cout << "----StarSystem " << starsystemindex << ": creating empty SystemBody " << systembodyindex << endl; } }; class Star : public SystemBody { public: Star ( int bodyindex, int systemindex ) : SystemBody ( bodyindex, systemindex ) { cout << "----StarSystem " << starsystemindex << ": converting empty SystemBody into Star " << systembodyindex << endl; } }; class Planet : public SystemBody { public: Planet ( int bodyindex, int systemindex ) : SystemBody ( bodyindex, systemindex ) { cout << "----StarSystem " << starsystemindex << ": converting empty SystemBody into Planet " << systembodyindex << endl; } }; class ExitNode : public SystemBody { public: ExitNode ( int bodyindex, int systemindex ) : SystemBody ( bodyindex, systemindex ) { cout << "----StarSystem " << starsystemindex << ": converting empty SystemBody into ExitNode " << systembodyindex << endl; } }; class StarSystem { public: int starsystemindex; vector<StarSystem> connectedlist; vector<Planet> planetlist; StarSystem ( int index ) { starsystemindex = index; cout << "--Creating StarSystem: " << starsystemindex << endl; int numberofbodies = (rand() % 4 ) + 2; for ( int i = 0; i <= numberofbodies; i +=1 ) { if ( i == 0) { Star body(i, starsystemindex); } else if ( i == numberofbodies ) { ExitNode body(i, starsystemindex); } else { Planet body(i, starsystemindex); } } } }; int main() { srand(time(0)); StarSystem starsystem0(0); return 0; }