2017-08-31 54 views
0

我正在製作一個程序,它使用地圖從文本文件中保存和組織音樂庫。藝術家地圖保存包含歌曲地圖的專輯地圖。我遇到的問題是,每個藝術家的每張專輯都插入到每位藝術家的下方,而不僅僅是該藝術家的專輯。我已經縮小到這個函數,它決定了藝術家是否是新的,如果它是插入藝術家地圖。一旦專輯被整理出來,專輯和歌曲也一樣。C++ STL在地圖上映射的地圖不能正確插入

這裏是我的類信息

class Song { 
public: 

    string title; 
    int time; 
    int track; 
}; 

class Album { 
public: 
    map <int, Song *> songs; 
    string name; 
    int time; 
}; 

class Artist { 
public: 
    map <string, Album *> albums; 
    string name; 
    int time; 
    int nsongs; 
}; 

這裏就是我相信問題是功能:

map <string, Artist*> newArtist(Artist* a, Album* al, Song* s, string artist, string album, int track, map <string, Artist*> lib){ 
    if (lib.find(artist) == lib.end()){ 
     cout << "New Artist: " << artist << endl; 
     a->name = artist; 
     lib[artist] = a; 
    } else if (lib.find(artist) != lib.end()){ 
     cout << "Old Artist: " << artist << endl; 
    } 
    if (lib.find(artist)->second->albums.find(album) == lib.find(artist)->second->albums.end()){ 
     cout << "New Album: " << album << endl; 
     al->name = album; 
     lib.find(artist)->second->albums[album] = al; 
    } else if (lib.find(artist)->second->albums.find(album) != lib.find(artist)->second->albums.end()){ 
     cout << "Old Album: " << album << endl; 
    } 
    return lib; 
} 

這是打印出來。正如你所看到的,每一張專輯都在每位藝術家之下。

King Crimson 
     A Russian Piano Recital 
     Intergalactic Boogie Express 
     Jazz Piano III (A Smithsonian Collection) 
     Josef Hofmann & Ignace Jan Paderewski Play Liszt 
     Larks' Tongues In Aspic 
     Paderewski Plays Concert No. 1 
     Three of a Perfect Pair 
Larkins, Ellis 
     A Russian Piano Recital 
     Intergalactic Boogie Express 
     Jazz Piano III (A Smithsonian Collection) 
     Josef Hofmann & Ignace Jan Paderewski Play Liszt 
     Larks' Tongues In Aspic 
     Paderewski Plays Concert No. 1 
     Three of a Perfect Pair 
League of Crafty Guitarists 
     A Russian Piano Recital 
     Intergalactic Boogie Express 
     Jazz Piano III (A Smithsonian Collection) 
     Josef Hofmann & Ignace Jan Paderewski Play Liszt 
     Larks' Tongues In Aspic 
     Paderewski Plays Concert No. 1 
     Three of a Perfect Pair 
Lewin, Michael 
     A Russian Piano Recital 
     Intergalactic Boogie Express 
     Jazz Piano III (A Smithsonian Collection) 
     Josef Hofmann & Ignace Jan Paderewski Play Liszt 
     Larks' Tongues In Aspic 
     Paderewski Plays Concert No. 1 
     Three of a Perfect Pair 
Paderewski, Ignace 
     A Russian Piano Recital 
     Intergalactic Boogie Express 
     Jazz Piano III (A Smithsonian Collection) 
     Josef Hofmann & Ignace Jan Paderewski Play Liszt 
     Larks' Tongues In Aspic 
     Paderewski Plays Concert No. 1 
     Three of a Perfect Pair 

任何想法,我在做什麼錯在這裏。

+0

您想告訴我們錯誤嗎? –

+0

好吧,我添加了輸出。 –

+0

我通過在函數之後刪除對象的刪除來獲得它的工作。 Idk如果這意味着我有內存泄漏 –

回答

0

爲了解決這個問題,我需要在主程序循環中創建對象,並且必須刪除刪除它們的語句。