Indoordinosaur
Indoordinosaur

Reputation: 127

C++ class templates and nested structs. "Invalid use of non-static data member..."

I am working on making a program and part of it requires the use of a BST template including a structure for the node. When I try to compile the code in g++ I get a compile error (and a few others that appear to be caused by the same problem. Below is the part of my code that is causing the problem.

 #include <iostream>

template <typename Comparable> class BinarySearchTree {
    public:
        BinarySearchTree();
        BinarySearchTree( const BinarySearchTree &rhs);
        ~BinarySearchTree();

        const Comparable & findMax() const;
        void insert( const Comparable &x);
        const BinarySearchTree & operator=(const BinarySearchTree &rhs);
        /* ..a bunch of other binary search tree related functions... */

    private:
        struct BinaryNode {
            Comparable element;/* why is this line causing problems? */
            BinaryNode *left;
            BinaryNode *right;

            BinaryNode(const Comparable &theElement, BinaryNode *lt, BinaryNode *rt);
             : element(theElement), left(lt), right(rt){}
        };
    /* ... some more functions ... */
};

int main() {
    return 0;
}

Compiling this with g++ causes the following error message:

Line 16:invalid use of non-static data member BinarySearchTree<Comparable>::BinaryNode::element

Apologies for the stupid question. This code is extremely similar to some code in my textbook and copying it just produces this error.

Upvotes: 1

Views: 950

Answers (1)

Brandon
Brandon

Reputation: 23498

BinaryNode(const Comparable &theElement, BinaryNode *lt, BinaryNode *rt); //<-- This..
             : element(theElement), left(lt), right(rt){}

has a semi-colon.. Remove it and it'll work just fine.

Upvotes: 10

Related Questions