编辑:在底部发布固定代码。感谢大家的帮助!
我刚刚学习 C++,但在继承方面遇到了麻烦。我已经搜索和搜索并尝试了所有可能的方法,但我无法在保留我希望它具有的功能的同时编译此代码。
我觉得我犯了一个愚蠢的错误,或者我可能只是错过了一些重要的概念,但如果有人能看一下它,我将非常感激!
如果我注释掉 StarSystem Constructor 中创建对象的 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;
}
最佳答案
也许这只是我,但你的构造函数是一个未定义的简单声明:
class StarSystem
{
public:
StarSystem(); // <--- Undefined!
您声明了一个构造函数,但没有定义此构造函数中实际发生的事情。
如果它是一个什么都不做的构造函数,就做
StarSystem () {} // Defined.
// Nothing happens inside, but everything gets default-constructed!
作为旁注,在发布此类内容时,发布错误编号并添加评论或某种错误发生位置的指示符会有所帮助(这样我们就可以在您的巨大代码块中看到它) .
编辑: 重要说明,如果您根本不使用该构造函数,只需将其删除即可。
关于c++ 类继承,未定义对 'Class::Constructor()' 的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16005322/