编辑:在底部发布了固定代码。感谢大家的帮助!
我只是在学习 c++ 并且在继承方面遇到了麻烦。我已经搜索和搜索并尝试了任何我能做的事情,但我无法在保留我希望它拥有的功能的同时编译这段代码。
我觉得我犯了一个愚蠢的错误,或者我只是错过了一些重要的概念,但如果有人可以看看它,我会非常感激!
如果我注释掉 StarSystem 构造函数中创建对象的 3 行,那么它会编译,所以我知道这与问题有关。
#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;
}
编辑:
感谢大家的帮助!只需在此处发布固定代码,以防将来有人会发现这很有用。
#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;
}