Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.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++ 尝试创建对象时出现分段错误_C++_Pointers_Object_Segmentation Fault - Fatal编程技术网

C++ 尝试创建对象时出现分段错误

C++ 尝试创建对象时出现分段错误,c++,pointers,object,segmentation-fault,C++,Pointers,Object,Segmentation Fault,我正在尝试创建一个节点对象。但我有错。我不知道我做错了什么。这是我的密码- #include <bits/stdc++.h> #define MAX 1000 #define THRESHOLD 2 using namespace std; class Node{ int val, fr; Node* link; public: Node(){ this->link = NULL; }

我正在尝试创建一个节点对象。但我有错。我不知道我做错了什么。这是我的密码-

#include <bits/stdc++.h>
#define MAX 1000
#define THRESHOLD 2
using namespace std;

class Node{
    int val, fr;
    Node* link;
    public:
        Node(){
            this->link = NULL;
        }
        void set_val(int val){
            this->val = val;
        }
        void set_fr(int fr){
            this->fr = fr;
        }
        void set_link(Node* link){
            this->link = link;
        }
        Node* get_link(){
            return this->link;
        }
        void inc_fr(){
            this->fr++;
        }
        int get_val() const{
            return this->val;
        }
        int get_fr() const{
            return this->fr;
        }
};

void increment(list<Node> &nodes, int &value);
bool compare(const Node& first, const Node& second);
void print_list(list<int> a);
void print_node(Node a);
void prune(list<Node> &nodes, list<int> &values);
void sort_list(list<Node> &nodes, list<int> &values);
int get_fr(list<Node> nodes, int val);
void insert_into_tree(map< Node*,list<Node*> > &tree, map< int, Node* > &header, Node* head, list<int> values);
bool find_head(map< Node*,list<Node*> > tree, Node* head);

int main(){
    fstream file("my.txt");
    string s;
    int check[MAX] = {0};
    list<Node> nodes;
    map< Node*, list<Node*> > tree;
    map< int, Node* > header;
    Node* null = new Node();
    null->set_fr(0);
    null->set_val(-1);

    while(getline(file, s)){
        istringstream buf(s);
        string s1;
        while(getline(buf, s1, ' ')){
            istringstream buf1(s1);
            int value;
            buf1 >> value;
            if(!check[value]){
                Node* new_node = new Node();
                new_node->set_val(value);
                new_node->set_fr(1);
                nodes.push_back(*new_node);
                check[value] = 1;
            } else {
                increment(nodes, value);
            }
        }
    }

    for(list<Node>::iterator it=nodes.begin(); it!=nodes.end(); it++){
        if(it->get_fr() < THRESHOLD){
            nodes.erase(it);
            it--;
        }
    }
    file.clear();
    file.seekg(ios::beg);

    while(getline(file, s)){
        cout << "Please Wait..." << endl;
        istringstream buf(s);
        string s1;
        list<int> values;
        while(getline(buf, s1, ' ')){
            istringstream buf1(s1);
            int value;
            buf1 >> value;
            values.push_back(value);
        }
        prune(nodes, values);
        sort_list(nodes, values);
        print_list(values);
        insert_into_tree(tree, header, null, values);
    }
    cout << "printing head" << endl;
    for(map< Node*, list<Node*> >::iterator it=tree.begin(); it!=tree.end(); it++){
        print_node(*it->first);
        cout << "children : ";
        for(list<Node*>::iterator jt=it->second.begin(); jt!=it->second.end(); jt++){
            Node* x = *jt;
            cout << x->get_val() << ' ';
        }
        cout << endl;
    }
    return 0;
}

bool compare(const Node& first, const Node& second){
    if(first.get_fr() > second.get_fr()){
        return true;
    } else{
        return false;
    }
}

void increment(list<Node> &nodes, int &value){
    for(list<Node>::iterator it = nodes.begin(); it!=nodes.end(); it++){
        if(it->get_val() == value){
            it->inc_fr();
            break;
        }
    }
}

void print_list(list<int> a){
    cout << "item set ";
    for(list<int>::iterator it = a.begin(); it!=a.end(); it++){
        cout << *it << " ";
    }
    cout << endl;
}
void print_node(Node a){
    cout << "item " << a.get_val() << endl;
    cout << "frequency " << a.get_fr() << endl;
}

void prune(list<Node> &nodes, list<int> &values){
    for(list<int>::iterator it=values.begin(); it!=values.end(); it++){
        if(get_fr(nodes, *it) < THRESHOLD){
            values.erase(it);
            it--;
        }
    }
}

void sort_list(list<Node> &nodes, list<int> &values){
    for(list<int>::iterator it=values.begin(); it!=values.end(); it++){
        for(list<int>::iterator jt=values.begin(); jt!=values.end(); jt++){
            if(get_fr(nodes, *it) > get_fr(nodes, *jt)){
                swap(*it, *jt);
            } else if(get_fr(nodes, *it) == get_fr(nodes, *jt)){
                if(*it>*jt){
                    swap(*it, *jt);
                }
            }
        }
    }
}

int get_fr (list<Node> nodes, int val){
    int fr;
    bool found = false;
    for(list<Node>::iterator it=nodes.begin(); it!=nodes.end(); it++){
        if(it->get_val() == val){
            found = true;
            fr = it->get_fr();
            break;
        }
    }
    if(found){
        return fr;
    } else {
        return -1;
    }
}

void insert_into_tree(map< Node*,list<Node*> > &tree, map< int, Node* > &header, Node* head, list<int> values){
    if(header.find(head->get_val()) == header.end()){
        header[head->get_val()] = head;
    } else{
        Node* new_head = header[head->get_val()];
        new_head = new Node();
        while(new_head != NULL){
            new_head = new_head->get_link();
        }
        new_head->set_link(head);
    }
    if(values.empty()) return;
    Node* node;
    if(find_head(tree, head)){
        bool found = false;
        for(list<Node*>::iterator it=tree[head].begin(); it!=tree[head].end(); it++){
            node = *it;
            if(node->get_val() == values.front()){
                node->inc_fr();
                found = true;
                break;
            }
        }
        if(!found){
            node = new Node();
            node->set_val(values.front());
            node->set_fr(1);
            tree[head].push_back(node);
        }
    } else{
        node = new Node();
        node->set_val(values.front());
        node->set_fr(1);
        tree[head].push_back(node);
    }
    values.pop_front();
    insert_into_tree(tree, header, node, values);
}

bool find_head(map< Node*,list<Node*> > tree, Node* head){
    bool found = false;
    for(map< Node*,list<Node*> >::iterator it=tree.begin(); it!=tree.end(); it++){
        Node* node = &*it->first;
        if(node->get_val() == head->get_val()){
            found = true;
            break;
        }
    }
    return found;
}
我在set_link函数中得到错误。

在此函数中:

void set_link(Node* link){
    this->link = new Node();
    this->link = link;
}
将此->链接分配两次,第一次使用新创建的指向节点的指针,第二次使用link参数,会发生什么情况:

使用new创建的指针丢失,导致内存泄漏。 也许在代码的其他地方,您为之前作为参数传递给set_link函数的节点调用delete node,因此此->链接无效,当您尝试通过它访问数据时,会发生segfault。 SEGFULT的另一个原因可能是,在调用set_链接时,这是无效的,例如先前删除的或null


编辑:实际上,它在代码中是空的。

显示创建节点对象的实际代码。您看到这个->链接=新节点;这个->链接被覆盖了吗?好的,我已经删除了这一行-这个->链接=新节点;该死,显示你创建节点对象的实际代码,而不是全部代码!删除任何没有帮助的内容。您正在使用许多无效的迭代器-删除它会使其无效。你应该使用它=擦除它。我已经删除了这一行->链接=新节点;仍然给我同样的错误。@eddard.stark这意味着你在某处删除了指针,正如我在第2页中所假设的那样,我在这一行中得到了seg错误->this->link=link@eddard.stark在你的问题中添加导致错误的代码