Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.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++ 龟兔唐';t没有地址,C++;_C++ - Fatal编程技术网

C++ 龟兔唐';t没有地址,C++;

C++ 龟兔唐';t没有地址,C++;,c++,C++,地址问题。最喜欢的老老实实地试着调试,他一直在调试,最后又坏了。所以这是关于析构函数的,但是-见鬼-我不知道到底是什么。 或者可能是算法本身的问题。或者我是问题的根源,因为我看不到明显的东西或缺乏基本原理。你在LinkedList析构函数中遇到了问题。 当您将l[4]的next元素指定为l[1]时,基本上您有了循环(这似乎是您想要的)。 但最后一个元素(l[4])指向现有元素。 因此,在析构函数中,您尝试销毁l[1]两次:第一次是在删除l[0]之后,第二次是在删除l[4]之后。 您应该检查列表的

地址问题。最喜欢的老老实实地试着调试,他一直在调试,最后又坏了。所以这是关于析构函数的,但是-见鬼-我不知道到底是什么。
或者可能是算法本身的问题。或者我是问题的根源,因为我看不到明显的东西或缺乏基本原理。

你在
LinkedList
析构函数中遇到了问题。 当您将
l[4]
next
元素指定为
l[1]
时,基本上您有了循环(这似乎是您想要的)。 但最后一个元素(
l[4]
)指向现有元素。 因此,在析构函数中,您尝试销毁
l[1]
两次:第一次是在删除
l[0]
之后,第二次是在删除
l[4]
之后。
您应该检查列表的设计,尤其是销毁列表的方式。

尽量缩短代码,以便问题仍然存在。没有人会通读200行代码,也许值得推荐他为析构函数使用他已经制作的
pop
成员函数(这将使代码最多3-5行,可读性更高)。
#include <iostream>

using namespace std;

class Element
{
public:
    Element* next;
    double field;
};

class LinkedList
{
protected:
    Element* begin;
    int num;
public:
    int Number() {return num;}
    LinkedList() 
    {
        begin = NULL;
        num = 0;
    }
    LinkedList(LinkedList& L) 
    {
        begin = new Element;
        num = L.Number();
        begin = L.begin;
    }

    virtual void push(double a)
    {
        Element* cur = begin;
        if(begin==NULL)
        {
            num = 1;
            begin = new Element;
            begin->field = a;
            begin->next = NULL;
            return;
        }

        while(cur->next!=NULL)
        {
            cur = cur->next;
        }
        num ++ ;

        cur->next = new Element;
        cur->next->field = a;
        cur->next->next = NULL;
    }
    virtual double pop()
    {
        Element* cur = begin;
        Element* prev = NULL;

        if(begin==NULL)
        {
            return -1;
        }
        if(num==1)
        {
            double res = begin->field;
            num--;
            return res;
        }

        while(cur->next!=NULL)
        {
            prev = cur;
            cur = cur->next;
        }
        double res = cur->field;
        prev->next = NULL;
        delete cur;
        num--;
        return res;
    }

    ~LinkedList()
    {
        num = 0;
        Element* cur = begin;
        if(cur!=NULL)
            cur = cur->next;
        while(cur!=NULL)
        {
            Element* prev = cur;
            cur = cur->next;
            delete prev;
        }
        if(begin!=NULL)
            delete begin;
    }
    Element* operator[](int i)
    {
        if(i<0 || i>num) return NULL;
        int k = 0;
        Element* cur = begin;
        for(k=0;k<i;k++)
        {
            cur = cur->next;
        }
        return cur;
    }
};

class R_LinkedList: public LinkedList{
public:

    double pop() {
        double res = begin->field;
        begin = begin->next;
        num--;
        return res;
    }
    bool cycle_search() {
        Element* tortoise = begin, *hare = begin;

        while(tortoise && hare && hare->next) {
            tortoise = tortoise->next;
            hare = hare->next->next;
            if(tortoise == hare) {
                cout << "Got loop" << endl;
                return 1;
            }           
        }
        cout << "No loop" << endl; return 0;
    }
};

int main()
{
    R_LinkedList l;
    l.push(2); l.push(3); l.push(6);l.push(3);l.push(6);
    cout  << l[2]->field << endl;
    l[4]->next = l[1];
    l.cycle_search();
    return 0;
}
HEAP[TestConsole2.exe]: Invalid address specified to RtlValidateHeap( 002E0000, 002EF8F0 )