Original Post
Hey, guys this is my first post here and i want to ask something. Here's the code: My program crashes and I don't know why. I marked the line in which program crashes. Thanks! [Edited by - Zahlman on December 20, 2009 8:08:19 PM]
#ifndef LIST_H
#define LIST_H
template <class CType>
class List
{
public:
List()
{
count=0;
}
//-------------------
int AddToList(CType item)
{
if (count==0)
{
ListMain= new CType;
ListMain[count]=item;
count++;
}
else if(count==1)
{
SecondList=new CType;
SecondList[0]=ListMain[0];
delete ListMain;
ListMain=new CType[count+1];
ListMain[0]=SecondList[0];
ListMain[1]=item;
delete SecondList;
count++;
}
else
{
SecondList= new CType[count];
for (int i=0; i<count; i++)
{
SecondList=ListMain;
}
delete [] ListMain;
count++;
ListMain= new CType[count];
for (int i=0; i<count-1; i++)
{
ListMain=SecondList;
}
ListMain[count-1]=item;
delete [] SecondList;
}
return count-1;
}
//-------------
void PrintValues()
{
for(int i=0; i<count; i++)
cout<<ListMain<<endl;
}
//--------------
CType GetItem(int index)
{
return ListMain[index]; <-------------------- my program breaks here.
}
//--------------
int GetItemsAmount()
{
return count;
}
//----------------
private:
CType *ListMain;
CType *SecondList;
int count;
};
#endif