phi*_*ath 9 c++ inheritance class
编辑:在底部发布固定代码.谢谢大家的帮助!
我只是在学习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;
}
Run Code Online (Sandbox Code Playgroud)
编辑:
感谢大家的帮助!只是在这里张贴固定代码,以防将来任何人发现这个有用.
#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;
}
Run Code Online (Sandbox Code Playgroud)
小智 13
也许只是我,但你的构造函数是一个简单的declration,没有定义:
class StarSystem
{
public:
StarSystem(); // <--- Undefined!
Run Code Online (Sandbox Code Playgroud)
您已声明了构造函数,但是没有定义此构造函数中实际发生的内容.
如果它是一个什么都不做的构造函数,那就做吧
StarSystem () {} // Defined.
// Nothing happens inside, but everything gets default-constructed!
Run Code Online (Sandbox Code Playgroud)
作为旁注,当发布这些类型的东西时,它有助于发布错误编号并发表评论或某种指示错误发生的位置(因此我们可以在您的巨大代码中看到它).
编辑: 作为一个重要的注释,如果您根本不使用该构造函数,只需删除它.