Question

I'm trying to create a binary tree structure. I have a Node class, and a Tree class which manipulates the Nodes. Im getting this error when i compile and I can't figure out what's wrong.

This is my node class..

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;
};

It is in member function 'insert_new_node()' that I am getting the error. Here is the implementation..

//------------------------------------------
//-- 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;
        }
    }
}

The Error Copied:

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() ) )
                        ~~~~~^ ~~~~~~~~

I have looked at other answers here, relating to this error, but i haven't found anything that helps me yet.

Any help would be greatly appreciated. Thanks

Was it helpful?

Solution

The -> is happening before the *, so the compiler is trying to use ->get_data on a Node<NODETYPE> ** which doesn't work.

Instead of

*pNode->get_data()

use

(*pNode)->get_data()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top