我正在尝试修复我的“addSongtoPlaylist”功能。截至目前它不起作用,我不确定我的声明在函数中是否有误,但我现在有点卡在那个部分。该程序使用 GUI 从歌曲列表创建播放列表。我应该能够加载歌曲列表,添加、删除和播放表单中的歌曲。对于该功能,我们将不胜感激。
#include "globals.h" //some global variables are included here
#include <cstdlib> //standard c library
#include "mediaItem.h" //the mediaItem class (you need to write a class that extends this class)
#include "song.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
ifstream infile;
song mySong[MAX_SONGS];
int NumberOfSongs;
int myPlaylist[MAX_PLAYLIST];
int PlaylistSize; //counter for size of playlist in array
song *newPlaylist[MAX_PLAYLIST];
bool loadSongList(string filename)
{
infile.open(filename);
string tempArtist, tempTitle, tempLocation;
int i = 0;
if (infile.fail())
{
cout << "The file could not be opened." << endl;
return false;
}
else
{
cout << "File opened successfully." << endl;
infile >> NumberOfSongs;
infile.ignore(1);
while (!infile.eof())
{
getline(infile, tempArtist);
getline(infile, tempTitle);
getline(infile, tempLocation);
mySong[i].setArtist(tempArtist);
mySong[i].setTitle(tempTitle);
mySong[i].setLocation(tempLocation);
i++;
}
infile.close();
return true;
}
}
int getNumberOfSongsInSongList()
{
return NumberOfSongs;
}
string getSongNameFromSongList(int songNum)
{
string tempArtist, tempTitle;
tempArtist = mySong[songNum].getArtist();
tempTitle = mySong[songNum].getTitle();
return tempArtist + " - " + tempTitle;
}
string getSongNameFromPlaylist(int playlistSpot)
{
string tempArtist, tempTitle;
tempArtist = mySong[myPlaylist[playlistSpot]].getArtist();
tempTitle = mySong[myPlaylist[playlistSpot]].getTitle();
return tempArtist + " - " + tempTitle;
}
void addSongToPlaylist(int songNum)
{
mySong[myPlaylist[PlaylistSize]].setArtist(mySong[songNum].getArtist());
mySong[myPlaylist[PlaylistSize]].setTitle(mySong[songNum].getTitle());
mySong[myPlaylist[PlaylistSize]].setLocation(mySong[songNum].getLocation());
mySong[myPlaylist[PlaylistSize]] = mySong[songNum];
newPlaylist[PlaylistSize] = &mySong[songNum];
PlaylistSize++;
}
int getNumberOfSongsInPlaylist()
{
return PlaylistSize;
}
void moveSongDownInPlaylist(int playlistSpot)
{
int temp;
if (playlistSpot >= PlaylistSize - 1)
return;
temp = myPlaylist[playlistSpot + 1];
myPlaylist[playlistSpot + 1] = myPlaylist[playlistSpot];
myPlaylist[playlistSpot] = temp;
}
void removeSongFromPlaylist(int playlistSpot)
{
for (int i = playlistSpot; i < PlaylistSize; i++)
moveSongDownInPlaylist(i);
myPlaylist[PlaylistSize - 1] = 0;
PlaylistSize--;
}
void clearPlaylist()
{
for (int i = 0; i < PlaylistSize; i++)
myPlaylist[i] = 0;
PlaylistSize = 0;
}
void moveSongUpInPlaylist(int playlistSpot)
{
int temp;
if (playlistSpot == 0)
return;
temp = myPlaylist[playlistSpot - 1];
myPlaylist[playlistSpot - 1] = myPlaylist[playlistSpot];
myPlaylist[playlistSpot] = temp;
}
void playSongFromPlaylist(int playlistSpot)
{
mySong[myPlaylist[playlistSpot]].playMedia();
}
void pauseSongFromPlaylist(int playlistSpot)
{
mySong[myPlaylist[playlistSpot]].pauseMedia();
}
void stopSongFromPlaylist(int playlistSpot)
{
mySong[myPlaylist[playlistSpot]].stopMedia();
}
最佳答案
我怀疑您的 addSongToPlaylist
函数只需要将 songNum
添加到 myPLaylist
的末尾。
看起来 myPlaylist
应该只是一个 int
数组,而不是 int *
。注意:此时您分配的是一个指针数组,而不是为指针分配内存。
列表中的每个 int
都是引用 mySong
中现有条目的歌曲编号。
我的建议:
- 将
myPlayList
更改为int
数组并修复所有取消引用它的代码(无处不在的*
)。< - 更改
addSongToPLaylist
以仅将歌曲编号添加到数组的末尾,它由(容易混淆的名称)PlayListSize
表示。
如果之后有问题,请提出。
编辑:
因为 myPlaylist
需要是 song *myPlaylist[MAX_SONGS]
,你仍然可以像上面那样做,但不是将歌曲编号添加到列表的末尾,你将在 mySong
数组中添加指向歌曲的指针。像这样的东西:
void addSongToPlaylist(int songNum)
{
// TODO: Check PlaylistSize is less than MAX_PLAYLIST
myPlaylist[PlaylistSize] = &mySong[songNum];
PlaylistSize++;
}
关于c++ - 如何使用类将歌曲添加到播放列表程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26835770/