Sieganie do skladowych z Tablicy bazowej zawierajacej obiekty pochodne

0

Problem polega na tym, że gdy staram się siegnąć po składowe w obiektów w tej tablicy to wysypuje mi się program. Z poczatku podejrzewalem błąd w algorytmie w funkcji składowej klasy Human AddPerson() ale po 1000krotnym analizowaniu nie moge dostrzec bledu, wiec zakladam blad merytoryczny. Wczesniej wskaznik na tablice wskaznikow humanList klasy Human zastapilem wskaznikiem na tablice dynamiczna na obiekty klasy bazowej i choc przypisywanie klas pochodnych nie bylo sygnalizowane przez kompilator jako blad to mialem podobne bledy z pamiecia, zmienilem forme na tablice wskaznikow, bo doczytalem ze nie mozna do bazowych przypisywac obiektow pochodnych ze wzgledu na inna pojemnosc w pamieci. Jak zatem siegac w takim przypadku do skladnikow klasy bazowej i klasy pochodnej?

błąd o tresci:
Unhandled exception at 0x00bc12fc in Human.exe: 0xC0000005: Access violation reading location 0x00000021.

Wystepuje gdy wywoluje funkcje GetPerson() na rzecz skladowej wskaznikowej na tablice wskaznikow do klasy pochodnej Student klasy bazowej Human

Bardzo prosze o pomoc jestem totalnie bezradny(choc domyslam sie, że błąd nie jest jakis mega skomplikowany). Dziekuje!

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

class Student;

class Human
{
	public:

		string name;
		static int counter;
		Human** humanList;

		Human& operator=(const Student* h){return *this;};

		void AddPerson()
		{
			if(counter > 1)
			{
				Human** tempList = new Human*[counter+1];
				for(int i = 0; i < counter; i++)
					tempList[i] = humanList[i];
				delete []humanList;
				humanList = new Human*[counter];
				
				for(int i = 0; i < counter; i++)
				{	
					humanList[i] = tempList[i];
				}
				delete []*tempList;
			}
			else
			{
				humanList = new Human*[counter];
			}

			counter++;
		};

		void SetPerson()
		{
			cout << "Podaj imie: ";
			cin >> name;
		};

		virtual void GetPrivate(){};

		void GetPerson()
		{
			for(int i = 0; i < counter; i++)
			{
				cout << humanList[0]->name << endl;
				humanList[0]->GetPrivate();
			}
		};

		Human()
		{
			humanList = NULL;
		}

		Human(int i)
		{
			humanList = NULL;
			SetPerson();
		}

};

class Student : public Human
{
	public:
		
		int indexNumber;

		Human& operator=(const Student* h){return *this;};

		void SetPrivate()
		{
			cout << "Podaj nr indeksu: ";
			cin >> indexNumber;
		};

		virtual void GetPrivate()
		{
			cout << "Numer indeksu: " << indexNumber << endl;
		}

		Student() : Human(1)
		{
			SetPrivate();
		};
};


int Human::counter(0);

int _tmain(int argc, _TCHAR* argv[])
{
	Human peoples;
	
	int choice;
	
	do
	{
		cout << "Dodaj osobe[1]" << endl;
		cout << "Wyswietl osoby[2]" << endl;
		cout << "Wyjdz z programu[5]" << endl;
		do
		{
			cout << "Wybor: ";
			cin >> choice;
		} while(!(choice == 1 || choice == 2 || choice == 5));

		switch(choice)
		{
			case 1:
				{
					cout << "Dodaj nowa osobe" << endl;

					peoples.AddPerson();
					Student temp;
					Student* wskTemp;
					peoples.humanList[Human::counter-1] = wskTemp;
				} break;
			case 2:
				{
					for(int i = 0; i < Human::counter; i++)
						peoples.humanList[i]->GetPerson();
				} break;
		}
	} while(choice != 5);

	system("pause");

	return 0;
}
 
2

odpowiem, jako ze nikt nie odpowiedzial.

int Human::counter(0);

nigdzie ten counter nie jest zwiekszany przez co ta linijka powoduje blad

 peoples.humanList[Human::counter-1] = wskTemp;

bo to jest rownowazne z

peoples.humanList[0-1] = wskTemp;

a na pewno nie masz obiektu w -1 i przez to jest blad (zapewne nie jedyny). Zamiast tablic stosuj std::vector

1 użytkowników online, w tym zalogowanych: 0, gości: 1