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;
}
zechciałby pan określić, jakie funkcje chcesz go mieć? – jepugs
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
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? –