C ++如何在其他类中创建类成员?

问题描述 投票:0回答:1

[当我试图在类的私有部分中创建类成员时,我得到“期望的类型说明符”。为什么它在WordCount类的私有部分中会出现这样的错误,但是在main()里面就可以了?

#include <iostream>

using namespace std;

template <class T>
struct AVLNode
{
    T element; //later might want to make this a T
    AVLNode* left;
    AVLNode* right;
    int height;

    //Constructor:
    AVLNode(T theElement, AVLNode<T>* lt, AVLNode<T>* rt, int ht = 0) //ht is short for height
        : element(theElement), left(lt), right(rt), height(ht) {}
};

template <class T>
class AVLTree
{
protected:
    AVLNode<T>* root = NULL;
    const T ITEM_NOT_FOUND;

public:
    AVLTree<T>(T notFound)
        : ITEM_NOT_FOUND(notFound), root(NULL) {}
};

class WordCount
{
private:
    AVLTree<int> tree(0); //Error
};

int main()
{
    AVLTree<int> avl(0);
}
c++ class syntax
1个回答
0
投票

您必须为成员initialization使用括号而不是括号:

非静态数据成员可以通过以下两种方式之一进行初始化:...通过默认的成员初始化程序,只是一个大括号或等于成员声明中包含的初始化程序,如果成员初始化程序列表中省略了该成员,则]

您可以尝试使用int进行寄生,但对int也无效。

© www.soinside.com 2019 - 2024. All rights reserved.