C++类模板和嵌套结构。 "Invalid use of non-static data member..."



我正在制作一个程序,其中一部分需要使用BST模板,包括节点的结构。当我尝试用 g++ 编译代码时,我收到一个编译错误(以及其他一些似乎是由同一问题引起的错误。下面是导致问题的代码部分。

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

使用 g++ 编译此内容会导致以下错误消息:

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

为愚蠢的问题道歉。这段代码与我教科书中的某些代码非常相似,复制它只会产生此错误。

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

有一个分号。.删除它,它会正常工作。

相关内容

最新更新