2016-04-10 8 views
0

私はC++を使ってリンクリストプロジェクトのコードを書いています。リンクリストとコンストラクタ

#include <iostream> 
#include <string> 
using namespace std; 
struct song1 
{ 
    string song; 
    string title; 
    song1* next; 
}; 
class song_list 
{ 
protected: 
    song1* head; 
    int length; 
public: 
    song_list(); 
    bool insertSong (song1* newSong, int track); 
    //bool removeSong (int track); 
    void printSong(); 
    ~song_list(); 
}; 
song_list::song_list() 
{ 
    head->song = "No data"; 
    head->title = "No data"; 
    head->next = NULL; 
    length = 0; 
} 
bool song_list::insertSong (song1* newSong, int track) 
{ 
    int count=0; 
    if ((track<=0) || (track>length+1)) 
    { 
     cerr<<"\nThe given track is out of range"; 
     return false; 
    } 
    if (head->next == NULL) 
    { 
     head->next= newSong; 
     length++; 
     return true; 
    } 
    count =0; 
    song1* p = head; 
    song1* q = head; 
    while (q) 
    { 
     if (count==track) 
     { 
      p->next = newSong; 
      newSong-> next = q; 
      length++; 
      return true; 
     } 
     p=q; 
     q=p->next; 
     count++; 
    } 
    if (count==track) 
    { 
     p->next = newSong; 
     newSong-> next = q; 
     length++; 
     return true; 
    } 
    cerr<<"Song was not added in the list"; 
    return false; 
} 
void song_list::printSong() 
{ 
    int count = 0; 
    song1* p=head; 
    song1* q=head; 
    cout<<"\n------------------\n"; 
    cout<<"Song playlist\n"; 
    while (q) 
    { 
     p = q; 
     cout<<"\n------------------\n"; 
     cout<<"\tPosition "<<count<<endl; 
     cout<<"\tsong "<<p->title<<endl; 
     cout<<"\tArtist "<<p->song<<endl; 
     q= p->next; 
     count++; 
    } 
} 
song_list::~song_list() 
{ 
    song1* p= head; 
    song1* q=head; 
    while (q) 
    { 
     p = q; 
     q = p->next; 
     if (q) delete p; 
    } 
} 

メインファイル:

#include <iostream> 
#include <string> 
#include "LinkedListh.h" 
using namespace std; 
int main() 
{ 
    int choice,repeat,trc; 
    song1* info; 
    song_list func; 
    do 
    { 
    cout<<"1. Add song "<<endl; 
    cout<<"2. Delete song "<<endl; 
    cout<<"3. Show song "<<endl; 
    cout<<"4. Search song "<<endl; 
    cin>>choice; 
    switch (choice) 
    { 
    case 1: 
      cout<<endl<<"Artist: "; 
      getline (cin,info->song); 
      //getline (cin,info->song); 
      cout<<endl<<"Song Title: "; 
      getline (cin,info->title); 
      cout<<"Song number: "<<endl;//ask the user to put the song number 
      //if artist doesnt exist the user should put 1. 
      //else user should put what number the song is. 
      cin>>trc; 
      func.insertSong(info,trc); 
     break; 
    case 3: 
     func.printSong(); 
     break; 
    } 
    cout<<"Repeat? 1.Yes 2.No"<<endl; 
    cin>>repeat; 
    }while (repeat == 1); 
    return 0; 
} 

コードは正常にビルドすることができますが、私はプログラムを実行すると、それは「動作を停止しました示し、これは、これまで

ヘッダファイル私のコードです"メッセージ。私はエラーが私のコンストラクタにあることを理解する。それは文字列を初期化する正しい方法ですか?なぜなら私がコンストラクタを消去すると、「曲とタイトルを最初に初期化する必要がある」というメッセージが出てきたからです。私はC++でまだ新しいので、ビットごとにリンクリストを学習しています。ちなみに、私はリンクされたリストを正しくやっていますか?ありがとう!

答えて

1

情報は、その曲のメンバーを作成しようとすると、割り当てられていないポインタです。

song1* info; 


getline (cin,info->song); 

逆参照しようとする前に、ポインタのメモリを実際に割り当てる必要があります。

それとも単に意味:

song1 info; 

してからちょうどinfo.songのように.->を変更します。

しかし、ほとんどの場合、他の問題もあるでしょう。

+0

'song_list'メンバー' head'は、私は、コンストラクタを使用してsong1のメモリを割り当てるしようとしていた同様の問題 – makadev

+0

を持ってあります。私はそれを正しく行っているかどうかはわかりません。 –

+0

"new ClassName()"と似たようなものがあるときは、コンストラクタが呼び出されます。曲1 *情報=新しい曲1();コンストラクタを呼び出してメモリをすべて確保します。しかし、スマートポインタは通常、allcoation yourlsef(std :: make_unique ();) – xaxxon

0

メモリを割り当てないうちに変数head-> songにデータを添付することはできません。

ビルド中にコンパイラでエラーが発生しないため、このエラーは実行時に発生します。

song_list::song_list() 
{ 
    head = new song1; // you have forgetten this line 
    head->song = "No data"; 
    head->title = "No data"; 
    head->next = NULL; 
    length = 0; 
} 

とsong1 * infoは同じです。メモリを割り当てる必要があります

0

既に述べたように、new演算子を使用してメモリを割り当てる必要があります。そのままでは、その型だけを指すポインタがあります。デザインに関する副作用:クラス内でstructを利用する方がよいでしょうか?あなたはそのクラスを扱っているだけで、それはパブリックメンバーです。あなたのクラスでリストを作成させてください。

+0

を実行するより簡単ですか?申し訳ありません、私はわずか数日前にデータ構造を学び始めました...私はC++を使用していましたが、このクラスはまったく新しいものです:) –

+0

私もこれを学んでいます。私はあなたの構造体がリストクラスによって構築されるべきだと言っていました。私はここに同様の投稿を持っています:http://codereview.stackexchange.com/a/125323/73806 –

0

多くのエラーチェックがありませんでした。クラスsong_listのデザインを最適化または変更する必要があります。 問題はありません。ちょうど今学び始めたので、涼しいままです。あなたの努力の功績。

ジャストメイン機能

int main() 
{ 
    int choice,repeat,trc; 
    song1* info; 
    song_list func; 
    info = new info; 

上の情報のためのメモリを割り当てようと次に

song_list::song_list() 
{ 
    head = new song1; 
    head->song = "No data"; 
    head->title = "No data"; 
    head->next = NULL; 
    length = 0; 
} 

にコードを修正してsong_list

のコンストラクタを使用してヘッドのためのメモリを割り当てます。 ......

メモリリークがここ

song_list::~song_list() 
{ 
    song1* p= head; 
    song1* q=head; 
    while (q) 
    { 
     p = q; 
     q = p->next; 
     if (q) delete p; // what happens for the last element if you check lastnode->next (would be null). the memory is lost. 
    } 
}