[英]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.