다음 코드를 사용하여 링크 된 목록을 구현하려고하는데 세그먼트 오류가 어디서 발생했는지 알려주십시오.gcc에서 포인터로 인한 세그먼트 오류 발생
void display()
{
node *temp=n;
while(temp->nextnode==NULL)
{
cout<<temp->info;
}
}
가 있어야한다 :
나는 우분투 GCC 컴파일러를 사용하고,
#include<iostream>
using std::cout;
using std::cin;
class ll
{
struct node
{
int info;
node *nextnode ;
}*n;
public:
ll()
{
n=NULL;
}
void getinfo()
{
node *temp,*r;
if(n==NULL)
{
temp=new node;
cout<<" \n enter the first elements of linklist \n";
int z;
cin>>z;
//i guess problem starts here
temp->info=z;
cout<<"the value of info is";
temp->nextnode = NULL;
n=temp;
}
else{
temp=n;
cout<<"heheh balls";
while(temp->nextnode==NULL)
{
temp=temp->nextnode;
}
r=new node;
cout<<"enter the element \t";
int y;
cin>>y;
r->info=y;
r->nextnode=NULL;
temp=r;
}
}
void display()
{
node *temp=n;
while(temp->nextnode==NULL)
{
cout<<temp->info;
}
}
};
int main()
{
ll p;
int v;
cout<<"enter the number of elements to be added to linklist \t";
cin>>v;
//tryn to input linklist from terminal
for(int i=0;i<v;i++)
{
p.getinfo();
}
p.display();
return 0;
}
코드를 선택하고 Ctrl-K를 눌러 코드를 다시 포맷하십시오. – chill
글쎄, GDB에서 실행하면 충돌이 발생한 줄과 충돌 할 때 모든 변수의 값을 알 수 있습니다. –
OliCharlesworth에게 감사하지만 GDB는 어디서 구할 수 있습니까? g ++로 빌드하면됩니다. – Imposter