编辑:在底部发布固定代码。谢谢大家的帮助!
我正在学习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;
}
答案 0 :(得分:13)
也许这只是我,但你的构造函数是一个简单的declration,尚未定义:
class StarSystem
{
public:
StarSystem(); // <--- Undefined!
您已经声明了构造函数,但是没有定义此构造函数中实际发生的内容。
如果它是一个什么都不做的构造函数,那么
StarSystem () {} // Defined.
// Nothing happens inside, but everything gets default-constructed!
作为旁注,在发布这些类型的东西时,有助于发布错误编号并发表评论或某种指示错误发生的位置(因此我们可以在您的巨大代码中看到它)
修改强> 重要的是,如果您根本不使用该构造函数,只需将其删除即可。
答案 1 :(得分:3)
您认为因为您没有调用SystemBody()
而不需要定义它。但是你间接地称它为。
并且不要
SystemBody() {};
建议。这不是你想要的。如果您不使用它,请完全删除它。
您的星级继承自SystemBody
。这意味着当构造新的Star
时,将调用SystemBody
的构造函数。
这一行
Star(int systembodyindex, int starsystemindex)
{
实际编译为
Star(int systembodyindex, int starsystemindex) :
SystemBody() // Here
{
如果你自己不调用SystemBody
,编译器会调用SystemBody
的默认构造函数。
如果您考虑一下,则需要在创建新Star
时以某种方式初始化Star(int systembodyindex, int starsystemindex) :
SystemBody(systembodyindex) // Here
{
。您可以明确地执行此操作,如
{{1}}
答案 2 :(得分:1)
您定义了许多默认构造函数但未实现它们。而不是
StarSystem(); // <- it is OK if you implement this somewhere but you didn't
写
StarSystem(){}
^^ this is empty implementation