2014-11-10 4 views
1

"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(); 
} 
+0

''작동하지 않습니다. ''는 유용한 진술이 아닙니다. * 무엇이 작동하지 않습니까? 그게 아니라는 게 뭐야? 컴파일 타임이나 런타임에 오류 메시지가 표시됩니까? 디버거로 코드를 단계별로 실행 해 보았습니까? Visual Studio에는 뛰어난 디버깅 기능이 내장되어 있습니다. 사용법을 모른다면 지금은 배울 시간입니다. –

+0

문제를 해결하기에는 너무 많은 코드입니다. –

+0

노래를 GUI 양식 창에서 재생 목록에 추가해야하는데 노래 목록이로드되지만 노래를 선택하고 아무 것도 누르지 않으면 아무 일도 일어나지 않습니다. "개체 참조가 개체의 인스턴스로 설정되지 않았습니다."라는 오류 메시지가 나타납니다. –

답변

2

나는 당신의 addSongToPlaylist 기능은 단순히 myPLaylist의 끝에 songNum를 추가 할 필요가 있다고 생각한다.

또한 myPlaylistint이 아닌 int *의 배열이어야합니다. 참고 : 현재 포인터 배열을 할당하고 있지만 포인터에 대한 메모리는 할당하지 않습니다.

목록의 각 intmySong의 기존 항목을 참조하는 노래 번호가됩니다.

나의 추천 :

  • int 배열로 변경 myPlayList과 (어디에나있는 *)를 역 참조되는 모든 코드를 수정.
  • addSongToPLaylist을 변경하면 코드 끝에 으로 표시된 노래 끝에 번호가 추가됩니다.

그 후에 문제가 발생하면 문의하십시오.

편집 : myPlaylist으로 요구 song *myPlaylist[MAX_SONGS]로, 당신은 여전히 ​​위와 같이 동일한 기능을 수행 할 수 있지만, 오히려리스트의 마지막 곡 번호를 추가하는 대신, 당신은 mySong 배열의 노래에 대한 포인터를 추가합니다. 다음과 같음 :

void addSongToPlaylist(int songNum) 
{ 
    // TODO: Check PlaylistSize is less than MAX_PLAYLIST 
    myPlaylist[PlaylistSize] = &mySong[songNum]; 
    PlaylistSize++; 
} 
+0

PlaylistSize는 배열의 크기에 불과했지만 이것을 시도해 보겠습니다. –

+0

그게 무슨 뜻이야 -'PlaylistSize'는 배열의 크기가 아닙니다. 'MAX_PLAYLIST'는 배열의 크기입니다. 'PlaylistSize'는 당신이 현재 사용하고있는 배열의 양입니다. 대문자로 시작하는 것이 혼란 스러웠다 고 생각합니다. 상수 또는 '열거 형'처럼 보이게 만듭니다. –

+0

좋아, 네가 무슨 뜻인지 알 겠어. 나는 당신의 방식대로 코드를 시도했지만 더 이상 오류가 없지만 "playlist to playlist"버튼을 누르면 아무 일도 일어나지 않습니다. –