0

분할 오류의 원인을 찾을 수 없습니다. 나는 당신의 도움을 어떤 방식 으로든 정말 감사 할 것입니다. 미리 감사드립니다.클래스 및 친구 기능을 사용하는 연결된 목록 : 분할 오류

/다항식 구현을위한 기본 연결 목록입니다.

0

:

다항식의 인덱스와 COEFF를 입력 : 나는 연결리스트와 경험이 없기 때문에 이/

#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; 
} 

이 출력 그냥 실험입니다 분할 오류 (코어 덤프 됨)

단지 하나의 요소 (인덱스) 만 허용하고 종료됩니다.

답변

1

초기화되지 않은 포인터를 전달 중입니다.

힙 : 당신도 새와 일부 메모리를 획득하거나 스택에 둘 필요가

linked_list *obj1 = new linked_list; 

스택 : 일

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

. 첫 번째 옵션은 의미합니다. 감사!!! –