【发布时间】:2014-03-06 23:19:20
【问题描述】:
我是 C++ 的新手,目前正在我的 C++ 课程中从事关于动态分配的项目。我一生都无法弄清楚我在这个程序中的指针哪里出错了。我正在使用三个不同的.cpp 文件,我们应该制作一个基本的播放列表程序。
请不要给我任何其他混乱代码的提示,我只是想弄清楚指针:
(Playlist class, contains a dynamic array of songs)
Playlist::Playlist (int s) {
size = s;
list = new Song[size];
}
Playlist::~Playlist() {
delete [] list;
}
void Playlist::Add(Song * s) {
size++;
Song * newlist = new Song[size];
for(int i = 0; i < size - 1; i++) {
newlist[i] = list[i];
}
delete [] list;
newlist[size] = *s;
list = newlist;
}
(菜单程序)
switch (choice) {
case 'A': {
cout << "\nEnter a song title to add: ";
cin.getline(tempTitle,36);
cout << "Enter the artist's name: ";
cin.getline(tempArtist,20);
cout << "Enter the category of the song ([P]op, [R]ock, [A]lternative, [C]ountry, [H]ip Hop, or Parod[Y$
cin >> catStorage;
tempCat = StyleChoice(catStorage);
cout << "Enter the size of the song in kilobytes: ";
cin >> tempSize;
Song * tempS = new Song;
tempS->Set(tempTitle, tempArtist, tempCat, tempSize);
mainlist.Add(tempS);
delete tempS;
break;
}
每当我运行Add() 函数然后退出菜单程序时都会收到错误消息(我有一个“X”条件来结束保持菜单运行的while 循环)。谢谢!如果您需要有关任何功能的更多信息,请告诉我。
【问题讨论】:
标签: c++ pointers dynamic-allocation