编辑:在底部发布固定代码.谢谢大家的帮助!
我只是在学习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,systemindex ) { cout << "----StarSystem " << starsystemindex << ": converting empty SystemBody into Planet " << systembodyindex << endl; } }; class ExitNode : public SystemBody { public: ExitNode ( int 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; }
解决方法
也许这只是我,但你的构造函数是一个简单的declration,没有定义:
class StarSystem { public: StarSystem(); // <--- Undefined!
您已声明了构造函数,但是没有定义此构造函数中实际发生的内容.
如果它是一个什么也不做的构造函数,那就做吧
StarSystem () {} // Defined. // Nothing happens inside,but everything gets default-constructed!
作为旁注,当发布这些类型的东西时,它有助于发布错误号并发表评论或某种指示错误发生的位置(因此我们可以在您的巨大代码中看到它).