C++ 指针赋值后丢失的值

C++ 指针赋值后丢失的值,c++,pointers,C++,Pointers,此代码正在功能将\u节点添加到\u列表中中为This->min\u节点->right和This->min\u节点->left打印0。当我在以前的迭代中为这个变量分配了一些值时,我无法理解为什么会发生这种情况。在main中打印时,此值可用,但调用函数后,此值丢失 #include<iostream> #include<vector> #include<iterator> namespace a1{ /******************************

此代码正在功能
将\u节点添加到\u列表中
中为
This->min\u节点->right
This->min\u节点->left
打印
0
。当我在以前的迭代中为这个变量分配了一些值时,我无法理解为什么会发生这种情况。在
main
中打印时,此值可用,但调用函数后,此值丢失

#include<iostream>
#include<vector>
#include<iterator>

namespace a1{
/******************************Declarations*********************************/
class node{
    //Private member declaration
    //Public member declaration
    public: int key;
    public: int degree;
    public: bool mark;
    public: node * left;
    public: node * right;
    public: node * child;
    public: node * parent;
    //Private method declaration
    //Public method declaration
    public: node(int);
};

class heap{
    //Private member declaration
    private: int node_count;
    private: void add_node_to_the_list(node *);
    //Public member declaration
    public: node * min_node;

    //Private method declaration
    private: void consolidate();
    private: void fib_heap_link(node *, node *);
    private: void cut(node *, node *);
    private: void cascading_cut(node *);

    //Public method declaration
    public: heap();
    public: void fib_heap_insert(int);
    public: void fib_heap_union(heap &);
    public: heap & fib_heap_extract_min();
    public: void fib_heap_decrease_key(node *, int);
    public: void fib_heap_delete(node *);
    public: int get_node_count();
};

};//End of namespace a1
/****************************Definitions*************************************/

/****************************node functions here*****************************/

a1::node::node(int key){
    this->key       = key;
    this->degree    = 0;
    this->mark      = false;
    this->left      = NULL;
    this->right     = NULL;
    this->child     = NULL;
    this->parent    = NULL;
}


/****************************Heap functions here*****************************/

//Private methods

void a1::heap::add_node_to_the_list(node * temp){
    if(this->min_node == NULL){
        this->min_node = temp;
        this->min_node->right = this->min_node;
        this->min_node->left = this->min_node;
    }
    else{
        temp->right = this->min_node->right;
        temp->left = this->min_node;
        //this->min_node->right->left = temp;
        //this->min_node->right = temp;
    }
}

//Public methods

a1::heap::heap(){
    this->node_count = 0;
    this->min_node = NULL;
}

void a1::heap::fib_heap_insert(int key){
    a1::node temp(key);
    if(this->min_node == NULL){
        a1::heap::add_node_to_the_list(&temp);
        this->min_node = &temp;
    }
    else{
        std::cout << "Printing this->min_node->right : " << this->min_node->right << std::endl;
        std::cout << "Printing this->min_node->left : " << this->min_node->left << std::endl;
        a1::heap::add_node_to_the_list(&temp);
        if(this->min_node->key > temp.key){
            this->min_node = &temp;
        }
    }
    this->node_count += 1;
}

int a1::heap::get_node_count(){
    return this->node_count;
}


/**************************Debug functions***********************************/

using namespace a1;

void print_fib_heap(node * n){
    if(n == NULL) return;
    else{
        node * min_node;
        node * trav;
        bool flag = false;
        min_node = n;
        trav = n;
        while(flag == false || trav != min_node){
            flag = true;
            std::cout << "(" << "key = " << trav->key << " d = " << trav->degree;
            print_fib_heap(trav->child);
            std::cout << ")";
            trav = trav->right;
        }
    }
}

/**************************Main Function to test*****************************/

int main(){
    heap h1;
    h1.fib_heap_insert(2);
    std::cout << "From main h1.min_node->right: " << h1.min_node->right << std::endl;
    h1.fib_heap_insert(3);
    //print_fib_heap(h1.min_node);
    return 0;
}
#包括
#包括
#包括
名称空间a1{
/******************************声明*********************************/
类节点{
//非官方成员声明
//公众成员声明
公共密钥:int-key;
公众:国际学位;
公众:布尔马克;
公共:节点*左;
公共:节点*右;
公共:节点*子节点;
公共:节点*父节点;
//私有方法声明
//公开方法声明
公共:节点(int);
};
类堆{
//非官方成员声明
private:int node_count;
private:void将_节点添加到_列表(节点*);
//公众成员声明
公共:节点*最小节点;
//私有方法声明
私有:void consolidate();
private:void fib_heap_link(节点*,节点*);
私有:空切(节点*,节点*);
私有:无效级联切分(节点*);
//公开方法声明
public:heap();
public:void fib\u heap\u insert(int);
public:void fib_heap_union(heap&);
public:heap&fib_heap_extract_min();
public:void fib\u heap\u reduce\u key(node*,int);
public:void fib_heap_delete(节点*);
public:int get_node_count();
};
};//名称空间a1的结尾
/****************************定义*************************************/
/****************************这里的节点函数*****************************/
a1::node::node(int键){
这个->键=键;
这个->度=0;
此->标记=假;
此->左=空;
此->右=空;
此->子对象=NULL;
此->父项=空;
}
/****************************这里的堆函数*****************************/
//私有方法
void a1::heap::将\u节点添加到\u列表中(节点*temp){
如果(此->最小节点==NULL){
此->最小节点=温度;
此->最小节点->右=此->最小节点;
此->最小节点->左=此->最小节点;
}
否则{
临时->右侧=此->最小节点->右侧;
临时->左=此->最小节点;
//此->最小节点->右->左=临时;
//此->最小节点->右侧=温度;
}
}
//公共方法
a1::heap::heap(){
此->节点计数=0;
此->最小节点=NULL;
}
void a1::heap::fib\u heap\u insert(int键){
a1::节点温度(键);
如果(此->最小节点==NULL){
a1::heap::将\u节点\u添加到\u列表(&temp);
此->最小节点=&temp;
}
否则{
标准::cout右-左最小节点->键>临时键){
此->最小节点=&temp;
}
}
该->节点计数+=1;
}
int a1::heap::get_node_count(){
返回此->节点\u计数;
}
/**************************调试功能***********************************/
使用名称空间a1;
无效打印纤维堆(节点*n){
如果(n==NULL)返回;
否则{
节点*最小节点;
节点*trav;
布尔标志=假;
最小节点=n;
trav=n;
while(flag==false | | trav!=min_节点){
flag=true;

std::cout
此->min\u node=&temp
正在保存本地堆栈变量的地址,该地址在方法返回时将无效。结果将是未定义的


我建议对
a1::heap::fib\u heap\u insert()
中的对象使用
new

如果您有一个调试器,请学习如何使用它。您的意思是
this->min\u node=temp;
?(无运算符地址)不,那是在add node方法中。我指的是insert方法。我明白了。这是相同的问题,但不同的地方。将
&temp
传递到add方法最终会执行相同的错误逻辑。我注意到了这一点。将
&temp
保存到
min_node
的后续行应该根本不存在,并且我知道呃,节点应该是新的。