0

セグメンテーションフォルトの理由が見つかりません。私は本当にあなたの助けに感謝します。よろしくお願いします。クラスとフレンド機能を使用したリンクリスト:セグメンテーションエラー

/多項式を実装するための基本的なリンクリスト。 これは単なる実験Iは、連結リスト/

#include<iostream> 
using namespace std; 
class linked_list 
{ 
//First element will hold index of the polynomial and the second element will hold its coefficient 
int a[2]; 
linked_list *p; 

public: 
    linked_list()    //Constructor to initialize to zero 
    { 
    a[0]=0; 
    a[1]=0; 
    p=NULL; 
    } 

    friend int start(linked_list *obj1) 
    { 
    cout<<"Enter the index and coeff of the polynomial:\n"; 
    cin>>obj1->a[0];  //Accepting the values of index and coeff 
    cin>>obj1->a[1]; 
    linked_list *obj2; 
    int garbage;   //A garbage just to hold the return value 
    char c; 
    cout<<"Enter y to continue or n to discontinue:\n"; 
    cin>>c; 
    if(c=='y') 
    { 
     obj1->p=obj2; //Setting pointer of first node to point to the second 
     garbage=start(obj2); 
     return garbage; 
    } 
    else 
    { 
     obj1->p=NULL; //Setting last pointer to NULL 
     return 0; 
    } 
} 

    friend void print(linked_list *obj1) 
{ 
    linked_list *temp;     //Temporary object pointer 
    cout<<obj1->a[0]<<"x^"<<obj1->a[1]; 
    temp=obj1;       
    while(temp->p!=NULL) 
    { 
     temp=temp->p;     //Temporary object pointer now holds address of next location 
     cout<<"+"<<temp->a[0]<<"x^"<<temp->a[1]; 
    } 
} 
}; 
int main() 
{ 
    int garbage; 
    linked_list *obj1; 
    garbage=start(obj1); 
    print(obj1); 
    return 0; 
} 

経験がないので、これが出力されている:

多項式の指標とCOEFFを入力します。

セグメンテーションフォールト(コアダンプ)

ただ1つの要素(インデックス)を受け入れ、終了します。

答えて

1

初期化されていないポインタを渡しています。

ヒープを:あなたは、どちらかの新しいと、いくつかのメモリを取得したり、スタックにそれを配置する必要があり

linked_list *obj1 = new linked_list; 

スタック:働い

linked_list obj1; 
garbage=start(&obj1); 
+0

。最初の選択肢は私が意味することです。ありがとう!!! –

関連する問題