Zapewne coś nie tak zrozumiałem i prubuje zrobić jakieś niedozwolone machloje...
mam klasy:
enum TYP {DOC = 'D', WWW = 'W', BOOK = 'B'};
class CDocument
{
protected:
// podstawowe dane dokumentu
std::string m_strAutor; // autor dokumentu
std::string m_strTytul; // tytuł dokumentu
tm m_Data; // data stworzenia
public:
// konstruktory
CDocument()
{
m_strAutor = m_strTytul = "???";
time_t Czas = time(NULL); m_Data = *localtime(&Czas);
}
CDocument(std::string strTytul)
{
CDocument(); m_strTytul = strTytul;
}
CDocument(std::string strAutor, std::string strTytul)
{
CDocument();
m_strAutor = strAutor;
m_strTytul = strTytul;
}
//-------------------------------------------------------------
virtual TYP TypDoc() const {return DOC;}
// metody dostępowe do pól
std::string Autor() const { return m_strAutor; }
std::string Tytul() const { return m_strTytul; }
tm Data() const { return m_Data; }
// pokazanie treści dok
virtual void PokazDokument(CDocument& pDocument);
};
class CBook : public CDocument
{
protected:
std::string m_strISBN; // numer ISBN książki
public:
// konstruktory
CBook(std::string strAutor, std::string strTytul)
{
m_strAutor = strAutor;
m_strTytul = strTytul;
}
CBook(std::string strAutor, std::string strTytul, std::string strISBN)
{
m_strAutor = strAutor;
m_strTytul = strTytul;
m_strISBN = strISBN;
}
//-------------------------------------------------------------
TYP TypDoc() const {return BOOK;}
// metody dostępowe do pól
std::string ISBN() const { return m_strISBN; }
// pokazanie treści dok
void PokazDokument(CBook& pDocument);
};
class COnlineDocument : public CDocument
{
protected:
std::string m_strURL; // adres internetowy dokumentu
public:
// konstruktory
COnlineDocument(std::string strAutor, std::string strTytul)
{
m_strAutor = strAutor; m_strTytul = strTytul;
}
COnlineDocument (std::string strAutor, std::string strTytul, std::string strURL)
{
m_strAutor = strAutor;
m_strTytul = strTytul;
m_strURL = strURL;
}
//-------------------------------------------------------------
TYP TypDoc() const {return WWW;}
// metody dostępowe do pól
std::string URL() const { return m_strURL; }
// pokazanie treści dok
void PokazDokument(COnlineDocument& pDocument);
};
I chcę zrobić listę skłądającą się z obiektów tych klas więc stworzyłem kolejną klasę:
class CLista
{
static const unsigned DOMYSLNY_ROZMIAR = 1;
protected:
unsigned Rozmiar;
CDocument* ListaDoc;
public:
// Konstruktor
CLista()
{
Rozmiar = DOMYSLNY_ROZMIAR;
ListaDoc = new CDocument[Rozmiar];
}
// modyfikatory
bool AddDocument(CDocument pDocument);
// metody dostępu
unsigned Ilosc() const {return Rozmiar;}
CDocument Show(unsigned indeks) const
{
if(indeks < Rozmiar)
{
return ListaDoc[indeks];
} else return false;
}
// Destruktor
~CLista(){delete[] ListaDoc;}
};
bool CLista::AddDocument(CDocument pDocument)
{
if(Rozmiar == 1)
{
ListaDoc[Rozmiar-1] = pDocument;
Rozmiar++;
return true;
}
else
{
CDocument* pnNowaTablica = new CDocument[Rozmiar+1];
//kopiowanie tresci
memcpy (pnNowaTablica, ListaDoc, (Rozmiar+1) * sizeof(CDocument));
delete[] ListaDoc;
// podmiana
ListaDoc = pnNowaTablica;
Rozmiar++;
//zapis obiektu na dodanym miejscu
ListaDoc[Rozmiar-1] = pDocument;
//usuniecie tymczasowej tablicy
delete[] pnNowaTablica; //<-- wyskakuje błąd*
// zwracamy pozytywny rezultat
return true;
};
}
i gdy sobie tworzę 2 przykładowe obiekty, chce je dodać do listy i wyswietlić np. Autora z obiektu bedącego jako pierwszy na liscie dostaje wałka :/
CDocument* Ksiazka = new CBook("KOLES","JAKIS_TYTUL","1111");
CDocument* Strona = new COnlineDocument("KOLES2","JAKIS_TYTUL2","www.sss.pl");
CLista* Spis = new CLista();
Spis->AddDocument(*Ksiazka);
Spis->AddDocument(*Strona);
std::cout << "Typ 1: " << (Spis->Show(0)).TypDoc() << std::endl;
std::cout << "Typ 2: " << (Spis->Show(1)).TypDoc() << std::endl;
*Unhandled exception at 0x1023d57c in Dokumenty.exe: 0xC0000005: Access violation writing location 0xcdcdcdcd.
może jakieś sugestie, podpowiedzi ?
z góry dzięki...