2013-04-14 15 views
5

編集:固定されたコードを下部に掲示します。みんなあなたの助けをありがとう!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; 
} 
+0

明示的にそうすることができます。どのような機能を持たせたいのですか? – jepugs

+0

私は、継承クラスstar、planet、およびexitnodeに共通のメンバ属性とメソッドを持つ汎用のsystembodyクラスを作成しようとしています。 – phimath

+0

コンストラクタでは、 'Star'、' ExitNode'、 'Planet'オブジェクトをスタックベースの自動変数として初期化しています。これらはすべてスコープから外れるとすぐに削除されます。あなたは何かのリストやベクトルにこれらを加えることを意味していますか? –

答えて

9

おそらくそれは私だけだが、ここにあなたのコンストラクタが定義されていないシンプルdeclrationです:

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

あなたが宣言したコンストラクタを持っていますが、実際にこのコンストラクタで何が起こっているのない定義はありません。

それだけで何もしないコンストラクタだ場合には、サイドノート、物事のこれらの種類を投稿するとき、それはエラー番号を掲示し、コメントやの指標のいくつかの種類を置くのに役立ちますと

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

を行いますどこでエラーが発生しているのですか(あなたの巨大なコードで見ることができます)。

編集: 重要なことに、そのコンストラクタをまったく使用していない場合は、削除するだけです。

+0

ありがとう、これは適切な定義ではありませんか? StarSystem(int index){....}? – phimath

+0

@ zacharydimariaいいえ、それは適切な定義です。しかし、あなたは '' StarSystem();が一番上に浮かんでいるので、2つのコンストラクタを持っています(少なくともC++はあなたのことを思っています)。それをコンストラクタにしたくない場合は、コードから完全に削除してください。 –

1

多くのデフォルトコンストラクタを定義しましたが、実装していませんでした。代わりに

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

書き込み

StarSystem(){} 
      ^^ this is empty implementation 
3

のあなたはSystemBody()を呼び出していないので、あなたがそれを定義する必要はありませんだと思います。しかし、あなたはそれを間接的に呼びます。

と提案されているよう

SystemBody() {}; 

を行うdo't。これはあなたが望むものではありません。代わりに、使用しない場合は完全に削除してください。


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

このライン

Star(int systembodyindex, int starsystemindex) 
    { 

は、実際に使用すると、1つを自分でコールしていない場合、コンパイラはSystemBodyのデフォルトコンストラクタを呼び出し

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

にコンパイルされます。


あなたが考えてみれば、あなたは新しいStarを作成するときに何とかSystemBodyを初期化する必要があります。

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

私のクラスStarはStarSystemではなくSystemBodyを継承していると思いましたか? – phimath

+0

@zacharydimariaああ私の悪い。私はそれを編集します。しかし状況は同じです。 – stardust

関連する問題