Может ли кто-нибудь помочь мне разобраться, что не так с моим оператором =? Без этих двух функций моя программа работает отлично, но как только они будут реализованы, это приведет к ошибке, завершающейся кодом завершения 11.
Я пытаюсь установить два связанных списка равными друг другу.< /p>
virticalList::virticalList(const virticalList &p2) {
*this=p2;
}
virticalList & virticalList::operator=(const virticalList& p2)
{
Node* temp = p2.head->getNext();
head = new Node(p2.head->getValue(),0);
Node* curr = head;
while(temp!=NULL){
curr->setNext(new Node());
curr->getNext()->setValue(temp->getValue());
temp = temp->getNext();
}
return *this;
}
Я думаю, проблема в моей функцииoper =. Или в заголовке закрытого участника.
Вот мой полный класс virticalList:
class virticalList
{
private:
Node* head = new Node();
public:
virticalList();
void print();
void virtInc();
~virticalList();
virticalList(const virticalList &p2);
virticalList& operator=(const virticalList& p2);
};
virticalList::virticalList()
{
head -> setValue(10);
Node * ptr = head;
for(int i = 10; i setNext(new Node());
ptr -> getNext()->setValue(i);
ptr -> getNext()->setNext(nullptr);
ptr = ptr -> getNext();
}
}
virticalList::~virticalList() {
Node * des = head;
Node * d = des->getNext();
while(des -> getNext()->getValue()!=NULL){
delete des;
des = d;
if(d->getNext()!= nullptr){
d = d->getNext();
}
}
}
void virticalList::print()
{
Node * print = head;
while(print -> getNext()->getValue()!=NULL){
cout getValue() getNext();
}
cout getNext()->getValue()!=NULL){
inc -> setValue(inc -> getValue()+1);
inc = inc -> getNext();
}
}
virticalList::virticalList(const virticalList &p2) {
*this=p2;
}
virticalList & virticalList::operator=(const virticalList& p2)
{
Node* temp = p2.head->getNext();
head = new Node(p2.head->getValue(),0);
Node* curr = head;
while(temp!=NULL){
curr->setNext(new Node());
curr->getNext()->setValue(temp->getValue());
temp = temp->getNext();
}
return *this;
}
Вот также для справки мой класс узла:
class Node
{
private:
int value;
Node* next;
public:
Node();
Node(int v, Node * next);
void setValue(int v);
int getValue();
Node* getNext();
void setNext(Node* theNewNext);
};
Node::Node()
{
next = 0;
value = 0;
}
Node::Node(int v, Node * next_in)
{
value = v;next = next_in;
}
void Node::setValue(int v)
{
value = v;
}
int Node::getValue()
{
return value;
}
Node* Node::getNext()
{
return next;
}
void Node::setNext(Node* theNewNext)
{
next = theNewNext;
}
Подробнее здесь: https://stackoverflow.com/questions/650 ... it-code-11