Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 与equals运算符有问题。以出口代码11结束_C++_Operator Overloading_Nodes_Equals_Operator Keyword - Fatal编程技术网

C++ 与equals运算符有问题。以出口代码11结束

C++ 与equals运算符有问题。以出口代码11结束,c++,operator-overloading,nodes,equals,operator-keyword,C++,Operator Overloading,Nodes,Equals,Operator Keyword,有人能帮我找出我的=操作员出了什么问题吗?如果没有这两个函数,我的程序运行得很好,但一旦它们被实现,就会导致错误,退出代码为11 我正在尝试将两个链接列表设置为彼此相等 virticalList::virticalList(const virticalList &p2) { *this=p2; } virticalList & virticalList::operator=(const virticalList& p2) { Node* temp = p

有人能帮我找出我的=操作员出了什么问题吗?如果没有这两个函数,我的程序运行得很好,但一旦它们被实现,就会导致错误,退出代码为11

我正在尝试将两个链接列表设置为彼此相等

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;
}
我认为问题出在运算符=函数中。或者在私人会员的头上

以下是我的完整Virticalist课程:

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<20; i++)
    {
        ptr -> 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 << print -> getValue() << " ";
         print = print -> getNext();
     }
     cout << "\n";
}

void virticalList::virtInc()
{
    Node * inc = head;
    while(inc -> 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;
}

显示的代码中有很多错误。我看到内存泄漏。我看到循环没有正确地迭代节点,最终会取消对
nullptr
s的引用。我看到复制构造函数被实现为调用
操作符=
,而不是反过来

我建议重写整件事,例如:

类节点
{
私人:
int值=0;
Node*next=nullptr;
公众:
节点(intv=0,Node*n=nullptr);
int getValue()常量;
无效设置值(int v);
节点*getNext()常量;
void setNext(节点*n);
};
Node::Node(int v,Node*n):
值(v),
下一个(n)
{
}
int节点::getValue()常量
{
返回值;
}
void节点::setValue(int v)
{
值=v;
}
Node*Node::getNext()常量
{
下一步返回;
}
void节点::setNext(节点*n)
{
next=n;
}
类病毒学家
{
私人:
节点*head=nullptr;
公众:
虚拟主义者();
virticalist(const-virticalist&p2);
~virticalist();
virticalist&operator=(const virticalist&p2);
无效打印()常量;
void virtInc();
};
virticalist::virticalist():
头部(新节点(10))
{
节点*ptr=头部;
对于(int i=11;i<20;++i)
{
ptr->setNext(新节点(i));
ptr=ptr->getNext();
}
}
virticalist::virticalist(const-virticalist&p2){
如果(p2.头){
节点*temp=p2.head;
节点*curr=head=新节点(temp->getValue());
同时(temp=temp->getNext()){
curr->setNext(新节点(temp->getValue());
curr=curr->getNext();
}
}
}
virticalist::~virticalist(){
节点*ptr=head,*next;
while(ptr){
next=ptr->getNext();
删除ptr;
ptr=下一个;
}
}
virticalist&virticalist::operator=(常量virticalist&p2)
{
如果(此!=&p2){
病毒温度(p2);
//标准:交换(压头、温度压头);
节点*ptr=头部;
压头=温度压头;
温度头=ptr;
}
归还*这个;
}
void virticalist::print()常量
{
节点*ptr=头部;
while(ptr){
cout getValue()getNext();
}
cout setValue(ptr->getValue()+1);
ptr=ptr->getNext();
}
}

如果您使
virticalist
成为
Node
朋友
,那么
virticalist
可以直接访问
Node::next
成员,您可以稍微简化
virticalist
构造函数:

类节点
{
私人:
int值=0;
Node*next=nullptr;
公众:
节点(intv=0,Node*n=nullptr);
int getValue()常量;
无效设置值(int v);
节点*getNext()常量;
void setNext(节点*n);
朋友级虚拟主义者;
};
Node::Node(int v,Node*n):
值(v),
下一个(n)
{
}
int节点::getValue()常量
{
返回值;
}
void节点::setValue(int v)
{
值=v;
}
Node*Node::getNext()常量
{
下一步返回;
}
void节点::setNext(节点*n)
{
next=n;
}
类病毒学家
{
私人:
节点*head=nullptr;
公众:
虚拟主义者();
virticalist(const-virticalist&p2);
~virticalist();
virticalist&operator=(const virticalist&p2);
无效打印()常量;
void virtInc();
};
virticalist::virticalist()
{
节点**ptr=&head;
对于(int i=10;i<20;++i)
{
*ptr=新节点(i);
ptr=&(*ptr)->next;
}
}
virticalist::virticalist(const-virticalist&p2){
节点**当前=&head;
节点*temp=p2.head;
while(临时){
*curr=新节点(temp->getValue());
curr=&(*curr)->next;
temp=temp->getNext();
}
}
virticalist::~virticalist(){
节点*ptr=head,*next;
while(ptr){
next=ptr->getNext();
删除ptr;
ptr=下一个;
}
}
virticalist&virticalist::operator=(常量virticalist&p2)
{
如果(此!=&p2){
病毒温度(p2);
//标准:交换(压头、温度压头);
节点*ptr=头部;
压头=温度压头;
温度头=ptr;
}
归还*这个;
}
void virticalist::print()常量
{
节点*ptr=头部;
while(ptr){
cout getValue()getNext();
}
cout setValue(ptr->getValue()+1);
ptr=ptr->getNext();
}
}

因此,如果您认为自己知道错误原因,您是否尝试过使用?还有,退出代码11是你得到的所有错误?因为如图所示:完整的错误消息更好,代码中仍然存在内存泄漏,就像今天早些时候一样。但是这不会导致它崩溃。@GrandJ是的,我得到的唯一错误是退出代码11
操作符=
设置了一个只有2个节点的列表(并在过程中泄漏了一堆节点),而不管有多少节点在
p2
中。然而,各种成员函数,如
print()
,期望列表至少包含10个节点;否则,它们将直接通过空指针。该代码中有许多可能的错误原因。有几个函数多次调用
getNext()
,但没有检查它是否返回null,此后每个调用都有未定义的行为。复制构造函数(用于初始化对象,对象的生命周期在构造函数完成后开始)调用该类的
操作符=()
函数(该函数假定
*此
是以前构造的对象)也是一种非常不寻常的做法。一种更常见的方法(例如复制和交换习惯用法)是让赋值操作符使用cop
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;
}