ana*_*acy 2 c++ templates data-structures
我正在尝试创建二叉树结构.我有一个Node类,以及一个操纵节点的Tree类.我编译时得到这个错误,我无法弄清楚什么是错的.
这是我的节点类..
template < typename NODETYPE >
class Node
{
friend class Tree<NODETYPE>;
friend class Queue<NODETYPE>;
private:
NODETYPE m_Data;
Node<NODETYPE> *m_pLeft;
Node<NODETYPE> *m_pRight;
Node<NODETYPE> *m_pNext;
public:
//-------------------------------------
//-- Constructor -
//-------------------------------------
Node( const NODETYPE &node_data )
:m_Data( node_data ),
m_pLeft( 0 ),
m_pRight( 0 ),
m_pNext( 0 )
{
};
//-------------------------------------
//-- Get Node Data -
//-------------------------------------
NODETYPE get_data() const { return m_Data; };
};
Run Code Online (Sandbox Code Playgroud)
我的树类..
template < typename NODETYPE >
class Tree
{
private:
Node<NODETYPE> *m_pRoot;
//--------------------------------------
//-- Utility Functions -
//--------------------------------------
void insert_helper( Node<NODETYPE> **pNode, const NODETYPE &node_data );
public:
//--------------------------------------
//-- Constructor / Destructor -
//--------------------------------------
Tree()
:m_pRoot( 0 ) {};
~Tree();
//--------------------------------------
//-- Public Member Functions -
//--------------------------------------
void insert_new_node( const NODETYPE &node_data );
void levelOrder_traversal() const;
};
Run Code Online (Sandbox Code Playgroud)
我在成员函数'insert_new_node()'中得到错误.这是实施..
//------------------------------------------
//-- Insert New Node In Tree -
//------------------------------------------
template < typename NODETYPE >
void Tree<NODETYPE>::insert_new_node( const NODETYPE &node_data )
{
insert_helper( &m_pRoot, node_data );
}
//------------------------------------------
//-- Insert New Node Helper -
//------------------------------------------
template < typename NODETYPE >
void Tree<NODETYPE>::insert_helper( Node<NODETYPE> **pNode, const NODETYPE &node_data )
{
if( *pNode == 0 )
{
*pNode = new Node<NODETYPE>( node_data );
}
else
{
if( node_data < ( *pNode->get_data() ) ) <---- LINE THAT THROWS ERROR
{
insert_helper( &(*pNode -> m_pLeft), node_data );
}
else if( node_data > *pNode -> get_data() )
{
insert_helper( &(*pNode -> m_pRight), node_data );
}
else
{
std::cout << "Node Value '" << node_data << "' is a duplicate"
<< std::endl;
}
}
}
Run Code Online (Sandbox Code Playgroud)
错误复制:
In file included from /home/ellipsis/c++_projects/school_projects/advanced_c++/final_exam/20.24/main.cpp:14:
/home/ellipsis/c++_projects/school_projects/advanced_c++/final_exam/20.24/TreeLib/Tree.cpp:84:34: error: member reference base type
'Node<double> *' is not a structure or union
if( node_data < ( **pNode->get_data() ) )
~~~~~^ ~~~~~~~~
Run Code Online (Sandbox Code Playgroud)
我在这里查看了其他答案,与此错误相关,但我还没有发现任何有用的东西.
任何帮助将不胜感激.谢谢
这->是在发生之前发生的*,所以编译器试图使用 - > get_data,Node<NODETYPE> **它不起作用.
代替
*pNode->get_data()
Run Code Online (Sandbox Code Playgroud)
使用
(*pNode)->get_data()
Run Code Online (Sandbox Code Playgroud)