我正在尝试创建二叉树结构。我有一个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; };
};
My Tree class ..
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;
};
它在成员函数&#39; insert_new_node()&#39;我收到了错误。 这是实施..
//------------------------------------------
//-- 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;
}
}
}
错误复制:
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() ) )
~~~~~^ ~~~~~~~~
我在这里看到了其他答案,与此错误相关,但我还没有发现任何对我有帮助的答案。
非常感谢任何帮助。感谢
答案 0 :(得分:9)
->
发生在*
之前,因此编译器正试图在Node<NODETYPE> **
上使用 - &gt; get_data,但这不起作用。
而不是
*pNode->get_data()
使用
(*pNode)->get_data()