編集:固定されたコードを下部に掲示します。みんなあなたの助けをありがとう!C++クラスの継承、未定義の 'Class :: Constructor()'への参照
私は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;
}
EDIT:あなたの助けをみんなに
感謝!将来的に誰かがこのコードを役に立つかもしれない場合に備えて、ここに固定コードを投稿するだけです。
#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;
}
明示的にそうすることができます。どのような機能を持たせたいのですか? – jepugs
私は、継承クラスstar、planet、およびexitnodeに共通のメンバ属性とメソッドを持つ汎用のsystembodyクラスを作成しようとしています。 – phimath
コンストラクタでは、 'Star'、' ExitNode'、 'Planet'オブジェクトをスタックベースの自動変数として初期化しています。これらはすべてスコープから外れるとすぐに削除されます。あなたは何かのリストやベクトルにこれらを加えることを意味していますか? –