链表值C ++的问题

时间:2016-06-01 02:49:08

标签: c++ pointers struct linked-list

我正在尝试自学c ++。这样做我为自己写了一个主要的查找应用程序的挑战。我使用效率较低的算法在python(学习python)中成功了一次。我正在使用双向链表来存储素数。目前我只是试图在一个线程中运行它,但我把它连接起来,所以我可以稍后多线程。

无论如何,TL; DR调试器显示程序卡住试图在Prime构造函数中为start链接的prm int赋值我已经做了一堆搜索但我无法弄清楚我是什么做错了。 (还要注意绑定是调试消息)

#include <iostream>
#include <math.h>
#include <cmath>

using namespace std;
using std::cout;

struct PLink{
    int prm;
    PLink *next;
    PLink *prev;
};   

class Prime{



    public:
        PLink *start, *end;

        Prime(){
            start -> prm = 2;
            end -> prm = 3;
            start->next = end;
            end->next = NULL;
            start->prev = NULL;
            end->prev = start;            
            addToEnd(5);
            cout <<"cbing" << endl;
        }
        void insert(int val){

        }     
        void addToEnd(int val){//adds a new prime to the end of the    list
            PLink *tmp = new PLink;
            tmp->prm = val;
            tmp->prev = end;
            end->next = tmp;
            tmp->next = NULL;
            tmp = end;
            cout << tmp->prm << endl;
            cout << "addbing" << endl;
        }   
        bool comp(int pot){ //compares the potential prime against known primes via modulo
            int lim = sqrt(pot);
            PLink * current = start;
            bool check = false;
            cout<<"bing " << pot << endl;
            while (current->prm < lim && check == false){
                if (pot%current->prm == 0) {
                    check = true;}
                current = current->next;                                                
            }
            return check; //false means its prime true means its not
        }

};

int main()
{
    Prime primeList;
    int cap = 10000;
    int beg = 5;
    int count = 3;
    bool toggle = false;
    bool check = false;
    cout << "2 \n3 \n5" << endl;
    while(count < cap){
        beg += 2;
        cout << "bing" << endl;
        if (toggle){
            beg += 2;}
        toggle = !toggle;
        check = primeList.comp(beg);
        if (check == false){
            primeList.addToEnd(beg);
            count++;   
            cout << "bing2" << endl;         
        }
    }    
};

1 个答案:

答案 0 :(得分:0)

using namespace std;
using std::cout;

第二个using std::cout;是多余的,您可以阅读一些有关C ++名称可见性的文档,如下所示:

http://www.cplusplus.com/doc/tutorial/namespaces/ http://www.tutorialspoint.com/cplusplus/cpp_namespaces.htm

Prime(){
    start -> prm = 2;
    end -> prm = 3;
    start->next = end;
    end->next = NULL;
    start->prev = NULL;
    end->prev = start;            
    addToEnd(5);
    cout <<"cbing" << endl;
}

注意:当你声明一个像PLink *start, *end; C ++编译器(比如'gcc'或clang)这样的指针时,只需要分配内存来存储该指针,但是不要分配内存来存储指针所指向的内容(这里就是你的意思) PLink对象)。

因此,您应该为这两个指针所指向的PLink对象分配内存:PLink *start, *end;,也就是说,您必须将上述代码更改为:

Prime(){
    start = new PLink(); // use the default constructor generated by C++ complier since you haven't declared one in struct PLink
    end = new PLink()
    start -> prm = 2;
    end -> prm = 3;
    start->next = end;
    end->next = NULL;
    start->prev = NULL;
    end->prev = start;            
    addToEnd(5);
    cout <<"cbing" << endl;
}

好吧,为了不造成内存泄漏并双重释放相同的指针,你应该小心操作你创建的对象。