11

編集:下部に修正コードを投稿しました。助けてくれてありがとう!

私はちょうど 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;
}
4

3 に答える 3

22

おそらくそれは私だけですが、ここのコンストラクターは定義されていない単純な宣言です:

class StarSystem
    {
        public:
            StarSystem(); // <--- Undefined!

コンストラクターが宣言されていますが、このコンストラクターで実際に何が起こっているかについての定義はありません。

何もしないコンストラクタの場合は、

StarSystem () {} // Defined.
// Nothing happens inside, but everything gets default-constructed!

補足として、これらの種類のものを投稿するときは、エラー番号を投稿し、コメントまたはエラーが発生した場所を示す何らかのインジケーターを挿入すると役立ちます (これにより、巨大なコードの塊でエラーを確認できます)。

編集: 重要な注意として、そのコンストラクターをまったく使用していない場合は、削除してください。

于 2013-04-14T23:00:33.770 に答える
3

呼び出しSystemBody()ていないので、定義する必要はないと思います。ただし、間接的に呼び出しています。

そして、しないでください

SystemBody() {}; 

提案どおり。これはあなたが望むものではありません。使用しない場合は、完全に削除してください。


あなたのクラス Star は から継承しSystemBodyます。つまり、 newStarが構築されると、コンストラクターSystemBodyが呼び出されます。

この行

Star(int systembodyindex, int starsystemindex)
    {

実際にコンパイルされます

Star(int systembodyindex, int starsystemindex) :
    SystemBody()   // Here
    {

SystemBody自分で呼び出さない場合、コンパイラはデフォルトのコンストラクターを呼び出します。


考えてみればSystemBody、新しい を作成するときに何らかの初期化を行う必要がありますStar。これを明示的に次のように行うことができます

Star(int systembodyindex, int starsystemindex) :
    SystemBody(systembodyindex)   // Here
    {
于 2013-04-14T23:04:49.553 に答える
1

多くのデフォルト コンストラクターを定義しましたが、それらを実装していませんでした。それ以外の

StarSystem(); // <- it is OK if you implement this somewhere but you didn't

書きます

StarSystem(){}
            ^^ this is empty implementation
于 2013-04-14T23:00:54.717 に答える