c++ - 如何使用类将歌曲添加到播放列表程序

标签 c++ visual-studio-2010 file class playlist

我正在尝试修复我的“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/

相关文章:

c++ - 使用 OpenGL 在 OpenCV 中绘图

c++ - 如何使用 TreeView_SetItem 改变 lParam?

linux - 在没有RAID1的情况下将文件从一个分区镜像到第二个磁盘上的另一个分区

c++ - 使用 SystemParametersInfo 函数设置桌面墙纸时出现问题

asp.net - Internet Explorer无法显示该网页

file - Golang 中的 bufio.NewScanner 是否读取内存中的整个文件而不是每个文件一行?

linux - 使用 Bash 或 Perl 将一行文本替换为目录中的多个文件

c++ - map 数组帮助c++/qt

c++ - 二叉树 BFS 的队列

c# - Visual Studio 2010 professional 时响应很慢