2013-04-14 9 views
5

Edycja: Wysłany poprawiony kod na dole. Dziękuję wszystkim za pomoc!Dziedziczenie klasy C++, niezdefiniowane odniesienie do 'Class :: Constructor()'

Właśnie uczę się C++ i mam problem z dziedziczeniem. Szukałem, szukałem i próbowałem wszystkiego, co mogłem, ale nie mogę skompilować tego kodu, zachowując funkcjonalność, którą chcę mieć.

Czuję, że robię głupi błąd, a może po prostu brakuje mi jakiejś wielkiej koncepcji, ale jeśli ktokolwiek mógłby na to spojrzeć, byłbym bardzo wdzięczny!

Jeśli skomentuję 3 linie w Konstruktorze StarSystem, które tworzą obiekty, to kompiluje się, więc wiem, że ma to związek z problemem.

#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:

dzięki wszystkim za pomoc! po prostu publikuj tutaj poprawiony kod na wypadek, gdyby ktokolwiek w przyszłości uznał to za przydatne.

#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

zechciałby pan określić, jakie funkcje chcesz go mieć? – jepugs

+0

Próbuję mieć ogólną klasę systembody, która będzie miała atrybuty i metody wspólne dla odziedziczonych klas gwiazda, planeta i exitnode. – phimath

+0

W konstruktorze, w którym inicjujesz obiekty 'Star',' ExitNode' i 'Planet' jako zmienne automatyczne oparte na stosie, wszystkie one zostaną usunięte, gdy tylko wykroczą poza zakres. Czy chcesz je dodać do jakiejś listy lub wektora? –

Odpowiedz

9

Może to tylko ja, ale konstruktora Oto prosty declration to nie zostało zdefiniowane:

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

masz konstruktor deklarowany, ale nie ma definicji tego, co rzeczywiście dzieje się w tym konstruktora .

Jeśli jest to konstruktor, który właśnie robi nic zrobić

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

Na marginesie, przy pisaniu tego typu rzeczy, to pomaga odpowiedzieć numer błędu i umieścić komentarz lub jakiś wskaźnik gdzie błąd się dzieje (możemy to zobaczyć w twojej gigantycznej kropli kodu).

EDYCJA: Co ważne, jeśli w ogóle nie korzystasz z tego konstruktora, po prostu go usuń.

+0

dziękuję, czy to nie jest właściwa definicja? StarSystem (indeks int) {....}? – phimath

+0

@zacharydimaria Nie, to jest właściwa definicja. Masz tylko 2 konstruktory, jednak (przynajmniej C++ sądzisz, że to robisz), ponieważ masz 'StarSystem();' unoszące się na górze. Jeśli nie chcesz, aby był to konstruktor, usuń go całkowicie z kodu. –

1

zdefiniowałeś wiele domyślnych konstruktorów, ale ich nie wdrożyłeś. Zamiast

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

zapisu

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

Myślisz, ponieważ nie wzywają SystemBody() nie trzeba go zdefiniować. Jednak nazywasz to pośrednio.

I do't zrobić

SystemBody() {}; 

jako sugerowane. To nie jest to, czego chcesz. Zamiast tego usuń go całkowicie, jeśli go nie używasz.


Twoja klasa dziedziczy z SystemBody Gwiazda. Oznacza to, że po zbudowaniu nowego modelu Star jest wywoływany konstruktor dla SystemBody.

Linia ta

Star(int systembodyindex, int starsystemindex) 
    { 

faktycznie jest kompilowany do

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

Kompilator wywołuje konstruktor domyślny SystemBody jeśli nie zadzwonić samemu.


Jeśli myślisz o tym trzeba zainicjować SystemBody jakoś podczas tworzenia nowego Star. Można to zrobić jawnie jak

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

Myślałem, że moja klasa Star odziedziczyła SystemBody, a nie StarSystem? – phimath

+0

@zacharydimaria o mój zły. Będę edytować. Ale sytuacja jest taka sama. – stardust

Powiązane problemy