Dynamically increasing an array in class

class Album{
private:
char albumName[100];
Song* List;
int numSongs;
public:
Album(char*);
~Album();
Album(const Album&);
Album& operator=(const Album&);
Song* findSong(char*);
int getAlbumLength();
void addSong(Song&);
void resize();
char* getArtist();
void deleteDuplicates();
void print();
};

//The constructor
Album::Album(char* alb){
strcpy_s(albumName,alb);
List=new Song[numSongs];
numSongs=0;
}

//And the functions
void Album::resize(){
Song* oldList=List;
List=new Song[numSongs*2];
for(int i=0;i<numSongs;i++)
{
List[i]=oldList[i];
}
delete[] oldList;
oldList=List;
}
void Album::addSong(Song& newsong){
List[numSongs]=newsong;
resize();
numSongs++;
}

-------------------------------------
And everytime I create the class it can have only one song because I set numSongs to zero.Dont know where im wrong.
You just made a design oversight. Use a dynamically allocated container instead of a static array, this will save you a lot of trouble. I'd suggest starting with 'std::vector' since it is one of the more flexible ones: http://www.cplusplus.com/reference/vector/vector/
In addition to what Computergeek01 said, you have a couple of problems:

1) In addSong, you're going to double the since of the array every time you add one song because you call resize every time. You should call resize only when the array is full. You're very quickly going to run out of memory since the size of List will increase exponentionally.

2) In your constructor, you allocating List for numSongs occurrances, but numSongs is uninitialized (garbage).

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Topic archived. No new replies allowed.