현재 출력에서 오류가 발생하고 이유를 모르겠습니다. 왜 누군가가 나를 안내 할 수 있다면 그것은 크게 감사 할 것입니다. 두 다항식을 함께 추가 할 수 있지만 출력을 얻을 때 출력 연산자에서 공백을 제거한 후 세그멘테이션 오류가 발생합니다. 나는 이것이 왜 있는지 모른다. 나는 그것이 도움이된다면 나는 또한 codeblocks를 사용하고있다.폴리 출력 오류 연결 목록
MAIN.CPP
#include <iostream>
#include "poly.h"
using namespace std;
int main()
{
int x1[] = {1 , 0 , 3 , 4 , 5};
int x2[] = {3 , 2};
polynomial p1(x1 , 4);
polynomial p2(x2 , 1);
polynomial p3(5);
polynomial p4;
polynomial result;
result = 6;
cout << " p1 = " << p1 << endl ;
cout << " p2 = " << p2 << endl ;
cout << " p3 = " << p3 << endl ;
cout << " p4 = " << p4 << endl ;
cout << " result = " << result << endl << endl ;
result = p1 + p2 ;
cout << " p1 + p2 = " << result << endl ;
poly.h
#include <iostream>
using namespace std;
class polynomial
{
struct node
{
int coefficient ;
node * link ;
};
public:
polynomial();
polynomial(const polynomial&);
polynomial(int* ,int);
polynomial(int);
~polynomial();
polynomial operator+(const polynomial&) const;
polynomial operator+(int) const;
const polynomial& operator=(const polynomial &);
const polynomial& operator=(int);
friend ostream& operator<<(ostream& outfile , const polynomial&);
friend polynomial operator+(int ,const polynomial&);
private:
node* head;
int degree;
};
poly.cpp
#include <iostream>
#include "poly.h"
using namespace std;
polynomial::polynomial()
{
head = new node;
head->coefficient = 0;
head->link = NULL;
degree = -1;
};
polynomial::polynomial(const polynomial& copy)
{
if(this != ©)
{
delete[] head;
head = copy.head;
}
};
polynomial::polynomial(int * p, int degree)
{
this->degree = degree;
head = new node;
head->coefficient = p[0];
head->link = NULL;
for(int x=1;x<degree;x++)
{
node* temp;
temp = new node;
temp->coefficient = p[x];
temp->link = head;
head = temp;
}
node* temp;
temp = new node;
temp->coefficient = p[degree];
temp->link = head;
head = temp;
};
polynomial::polynomial(int s)
{
degree = 0;
head = new node;
head->coefficient = s;
head->link = NULL;
};
polynomial::~polynomial()
{
node* temp = head;
node* current = head;
while(current != NULL)
{
current = current->link;
delete temp;
temp = current;
if (current == NULL || current == NULL)
break;
}
};
polynomial polynomial::operator+(const polynomial& rhs) const
{
polynomial hold;
polynomial tempLhs;
polynomial tempRhs = rhs;
tempLhs.degree = degree;
tempRhs.degree = rhs.degree;
hold.degree;
int tempDegree;
tempLhs.head = new node;
tempRhs.head = new node;
hold.head = new node;
for(int x=0;x<tempDegree+1;x++)
{
node* temp;
temp = new node;
temp->coefficient = 0;
temp->link = hold.head;
hold.head = temp;
}
tempLhs.head = head;
tempRhs.head = rhs.head;
if(tempLhs.degree < tempRhs.degree)
{
tempDegree = tempLhs.degree;
hold.degree = tempDegree;
for(int x = (tempDegree-tempLhs.degree-1);x<tempDegree+1;x++)
{
node* temp;
temp = new node;
temp->coefficient = 0;
temp->link = tempLhs.head;
tempLhs.head = temp;
}
}
else if(tempLhs.degree > tempRhs.degree)
{
tempDegree = tempLhs.degree;
hold.degree = tempDegree;
for(int x = (tempDegree-tempRhs.degree-1);x<tempDegree+1;x++)
{
node* temp;
temp = new node;
temp->coefficient = 0;
temp->link = tempRhs.head;
tempRhs.head = temp;
}
}
else
{
tempDegree = tempRhs.degree = tempLhs.degree;
hold.degree = tempDegree;
}
node* lhsCurrent = tempLhs.head;
node* rhsCurrent = tempRhs.head;
int tempArr[tempDegree];
while(lhsCurrent != NULL && rhsCurrent != NULL)
{
for(int x=tempDegree;x>-1;x--)
{
tempArr[x]= lhsCurrent->coefficient + rhsCurrent->coefficient;
lhsCurrent = lhsCurrent->link;
rhsCurrent = rhsCurrent->link;
}
}
polynomial use(tempArr, tempDegree);
return use;
};
polynomial polynomial::operator+(int rhs) const
{
polynomial temp = *this;
return rhs+temp;
};
const polynomial& polynomial::operator=(const polynomial& rhs)
{
cout << "doing = operator" << endl;
degree = rhs.degree;
if(this != &rhs)
{
delete[] head;
head = rhs.head;
}
return *this;
};
const polynomial& polynomial::operator=(int rhs)
{
degree = 0;
head = new node;
head->coefficient = rhs;
head->link = NULL;
};
ostream& operator<<(ostream& out, const polynomial& rhs)
{
out << "operator ";
polynomial::node* temp = new polynomial::node;
temp = rhs.head;
while(temp != NULL)
{
out << temp->coefficient << " ";
temp = temp->link;
if(temp == NULL)
break;
}
out << " ";
};
출력이
p1 = 5 x ^4 + x ^2 + 5 x + 4
p2 = 3 x + 2
p3 = 5
p4 = 0
result = 6
p1 + p2 = 5 x ^4 + x ^2 + 8 x + 6
되어야
이 결과를 얻고 있지만 학위가 올바르게 표시되도록 서식을 지정해야하지만 추가적으로 올바르게 출력됩니다. 문제가되지 않는 출력 연산자를 조정해야합니다. 내가 poly.cpp의 마지막 줄에 두 번째
out << " ";
하지 않고 프로그램을 실행할 때마다
오류가 발생합니다.
코드에서 삭제 될 때 < < 때 폴리 Cpppp의 마지막 줄을 일이 줄 215 이후에 세그먼트 오류가 있습니다.
생성자에 대한 관련없는 참고 사항 : 하나에 대한 필요성을 알았지 만 ... 'if (this! = = & copy)'가 항상 발생합니다. 복사 생성자에서 새로운'다항식'을 만듭니다. 이것은 오래된 다항식의 주소를 가질 수있는 방법이 없다는 것을 의미합니다. 이것은'head'까지 확장됩니다. 이 객체는 아주 새롭고 아직'head'를 설정하지 않았기 때문에 당신은 아마도'삭제 '할 머리를 가질 수 없습니다. 'delete'는 자신이 소유하지 않은 데이터를 삭제하려고 시도하기 때문에 실제로 많은 시간이 걸릴 것입니다. – user4581301
이 댓글은 약간 길었습니다. 죄송합니다. 마지막으로'head = copy.head; '는 복사 생성자의 기본 동작으로 돌아가므로 복사 생성자를 만드는 것을 무효로합니다. 복사 대상을 생각하지 않고 모든 멤버를 눈먼만큼 복사합니다. 이것은 노드를 복사하지 않으며, 헤드 노드에 포인터를 복사합니다. 이제 동일한 목록을 가리키는 두 개의 객체가 있습니다. 이것은 매우 나쁩니다. 하나의 사본을 수정하면 다른 사본이 수정됩니다. 한 장의 복사본을 삭제하면 다른 하나는 쓰레기 메모리를 가리키고 시간 소절이 생깁니다. – user4581301
'operator = '에 대한주의 사항 : 일단 복사 생성자가 작동하면 이것을 훨씬 쉽게 구현할 수 있습니다. 매우 자세한 내용은 [복사 및 스왑 (copy-and-swap) 관용구 란 무엇입니까?] (https://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom)을 참조하십시오. 다시 말하지만 포인터를 할당하고 노드를 복사하지 않습니다. 그건 그렇고, 당신의 붐을 불러옵니다. 그래서 결국이 모든 것이 관련이없는 것은 아닙니다. 어쩌면 정식 답을 써야할까요? – user4581301