[英]Class Pointer Losing Members?
我有一棵正在搜索的二叉树:
TreeNode<Vessel*>* node = this->tree_->search("PotatoFace");
string mystring = node->print();
当我运行它时,节点包含正确的数据,但是当我输入以下命令立即打印该数据时:
string TreeNode<T>::print()
{
return data_->toString();
}
“ this”(应该是“ node”,并且与“ node”具有相同的内存地址)的所有数据成员(包括Vessel *)都设置为null。
有任何想法吗?
谢谢!
全树节点:
#pragma once
#include <cstring>
#include <fstream>
#include <iostream>
using namespace std;
template <class T>
class TreeNode
{
private:
TreeNode<T>* greaterNode_;
TreeNode<T>* lessNode_;
TreeNode<T>* parentNode_;
TreeNode<T>* getLowest_();
T data_;
public:
TreeNode();
TreeNode(T data);
void add(T data);
bool operator==(const string &rhs);
TreeNode* search(T data);
void seqSearch(string data, TreeNode<T>* node);
void del(TreeNode<T>* root);
void toFile(ofstream& BSTFile);
TreeNode* compare(int sig[4]);
TreeNode* getRoot();
TreeNode* forward(TreeNode<T>* node);
string print();
};
template <class T>
TreeNode<T>::TreeNode(T data)
{
data_ = data;
greaterNode_ = lessNode_ = parentNode_= NULL;
}
template <class T>
TreeNode<T>::TreeNode()
{
}
template <class T>
void TreeNode<T>::seqSearch(string data, TreeNode<T>* node )
{
if(*data_ == data)
{
*node = this->data_;
}
if(this->lessNode_)
{
this->lessNode_->seqSearch(data, node);
}
if(this->greaterNode_)
{
this->greaterNode_->seqSearch(data, node);
}
}
template <class T>
string TreeNode<T>::print()
{
return data_->toString();
}
仍不完全确定如何解释为什么它不起作用,但这是一个范围问题,在二叉树类树节点之外丢失了数据。 取出所有返回了节点的树函数,现在一切正常。
您确定要写:
string mystring = node->print();
不
string mystring = hello->print();
如果是,似乎是
string mystring = node->print();
为空(节点为空)。 这可能有几个原因:
如果粘贴更多代码,这将非常有帮助。
仍不完全确定如何解释为什么它不起作用,但这是一个范围问题,在二叉树类树节点之外丢失了数据。
通过确保Binary Tree类不返回TreeNode *类型的任何内容并在二叉树类中完成节点的值运行后运行我想要的其他任何函数来进行纠正。 现在可以使用。 感谢您的帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.