Data/print 不相关数据的无限循环错误

Data/print error with infinite loop of irrelevant data

已编辑

我有一些问题,可能与析构函数有关,但我找不到问题所在。我是 cpp 的新手,所以我不确定为什么以及何时调用析构函数,但是当我尝试调试我的代码时,我没有发现任何问题并正确打印了所有内容,但是当我 运行 它没有调试它只打印 Hello World,仅此而已。 如果在调试中我没有看到它,我该如何解决这个问题? 问题出在哪里?

我的代码看起来像这样:(已编辑)

#include <iostream>
#include <cstring>
#include <string>
#include <functional>

#include "dummy.h"
using namespace std;

#ifndef SORT_H
#define SORT_H

template <class T>
class LinkedList {
    struct Node {
        Node(const T &in) : data(in) {}
        T data;
        Node * next;
    };

    class Iterator
    {
        Node *m_ptr;              // pointer to current node in the list
    public:
        Iterator(Node * node) {
            m_ptr = node;
        }
        Iterator& operator ++ () {
            m_ptr = m_ptr -> next();
            return *this;
        }
        Iterator operator ++ (T) {
            Iterator temp(*this);
            m_ptr = m_ptr -> next();
            return temp;
        }
        bool operator == (const Iterator other) const {
            return m_ptr == other.m_ptr;
        }
        bool operator != (const Iterator other) const {
            return m_ptr != other.m_ptr;
        }
        T& operator * () {
            return m_ptr -> data();
        }
        operator bool() {
            return m_ptr != 0;
        }
    };

    Node * head;

public:
    LinkedList() {
        head = nullptr;
    }
    LinkedList(T value) {
        head = new Node(value);
        head -> data = value;
        head -> next = nullptr;
    }

    ~LinkedList() {
        while(head != nullptr) {
            Node * n = head -> next;
            free(head);
            head = n;
        }
    }

    void operator = (T &t) {
        head = t.head;
    }

    Node * nodeCopy(Node * head) {
        if (head == nullptr) {
            return nullptr;
        }
        Node * copied_node = new Node(head -> data);
        copied_node -> data = head -> data;
        copied_node -> next = nodeCopy(head -> next);
        return copied_node;
    }

    LinkedList(LinkedList &list){
        head = nodeCopy(list.head);
    }


    template<typename B>
    LinkedList filter(LinkedList &list, B pred) {
        LinkedList <T> new_list(list);
        Node * curr = list.head;
        while (curr) {
            if (!(pred(curr -> data))) {
                new_list.remove(curr -> data);
            }
            curr = curr -> next;
        }

        return new_list;
    }

    int length() {
        int counter = 0;
        Node * tmp = head;
        while( tmp ) {
            counter++;
            tmp = tmp -> next;
        }
        return counter;
    }

    void insert(T value) {
        if (head == nullptr) {
            head = new Node(value);
//            head -> data = value;
//            head -> next = nullptr;
            return;
        }
        Node* n = new Node(value);

        Node* tmp = head;
        while (tmp != nullptr) {
           if (value > tmp -> data && tmp -> next != nullptr) {
               if (tmp -> next -> data > value) {
                   Node * curr = tmp -> next;
                   tmp -> next = n;
                   n -> next = curr;
                   return;
               } else {
                   tmp = tmp -> next;
               }
           } else if (value > tmp -> data && tmp -> next == nullptr) {
               tmp -> next = new Node(n -> data);
               n -> next = nullptr;
               tmp -> next = n;
               return;
           } else if (value == tmp -> data && tmp -> next == NULL) {
               tmp -> next = new Node(n -> data);
               n -> next = nullptr;
               tmp -> next = n;
               return;
           } else if (value == tmp -> data && tmp -> next != NULL) {
               n -> next = tmp -> next;
               tmp -> next = n;
               return;
           } else {
               n -> next = tmp;
               head = n;
               return;
           }
        }
    }

    void remove(T value) {
        Node * tmp = head;
        while (tmp) {
            if (tmp -> data < value && tmp -> next == nullptr) {
                return;
            } else if (tmp -> data < value && tmp -> next -> data == value) {
                Node * ptr = tmp -> next -> next;
                free(tmp -> next);
                tmp -> next = ptr;
                return;
            } else {
                tmp = tmp -> next;
            }
        }
    }

    void print() {
        Node * curr = head;
        while (curr) {
            cout << curr -> data << endl;
            curr = curr -> next;
        }
    }

    Iterator begin() {
        return head;
    }
    Iterator end() {
        Node * curr = head;
        while (curr) {
            if (curr -> next == nullptr) {
                return curr;
            }
            curr = curr -> next;
        }
    }
};


#endif

我的主要是:

#include <iostream>
#include "sortedList.h"
#include "dummy.h"

bool func(Dummy num) {
    int number = num.get();
    if (number % 2 != 0) {
        return false;
    }
    return true;
}

int main() {
    std::cout << "Hello, World!" << std::endl;

    Dummy teeth(24);
    teeth.add(7);
    Dummy slime(11);
    slime.add(1);
    Dummy josh(32);
    LinkedList<Dummy> teeth_list;
    teeth_list.insert(teeth);
    teeth_list.insert(slime);
    teeth_list.insert(josh);
    int num = teeth_list.length();
    cout << "The length is: " << num << endl;
    teeth_list.remove(josh);
    teeth_list.print();
    cout << "Now printing teeth_list list" << endl;
    teeth_list.insert(josh);

    teeth_list.print();

    LinkedList<Dummy> dummy;
    dummy = teeth_list.filter(teeth_list, func);
    cout << "Now printing dummy list after filtering" << endl;
    dummy.print();

    return 0;
}

在我尝试 运行 之后,控制台日志看起来像这样:

 C:\Users\User\CLionProjects\ex2.2\cmake-build-debug\exe_name.exe
Hello, World!

Process finished with exit code -1073741510 (0xC000013A: interrupted by 
Ctrl+C)

在标准库中,结束迭代器是容器末尾之后的一处。为了与标准库兼容,您的 end() 方法应该是:

Iterator end()
{
    return Iterator(nullptr);
}

写一些单元测试可能是个好主意,也许 Googletest。使用您的原始代码,此测试应该会失败:

ASSERT_EQ(instance.length(), std::distance(instance.begin(), instance.end()) 
    << "Distance between begin and end iterators should be the same as length";