模板化类中的C++模板成员变量具有不完整的类型

Aar*_*ron 3 c++ templates

我在头文件中有类似以下的代码:

template<class A>
class List {
  private:
    QVector<A> _list;
};
Run Code Online (Sandbox Code Playgroud)

其中QVector是标准的QT容器.

当我尝试在另一个头文件中创建List类型的变量作为成员变量时,如下所示:

class Model {
  private:
    List<int *> the_list;
};
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

In instantiation of 'List<int *>':
instantiated from here
error: 'List<A>::_list' has incomplete type
Run Code Online (Sandbox Code Playgroud)

基本上,我想要一个模板化的自定义列表,它使用内部QVector来存储数据项.

我假设我的语法有点偏,所以任何帮助都会受到赞赏.

Joh*_*ica 5

在声明之前确保你有#include头文件.如果省略它,那么它是一个未定义的类型,但由于是一个模板化的类,编译器在您第一次实例化之前不会省略错误消息.QVectorclass List { }QVectorListList

#include <QVector>

template<class A>
class List {
  private:
    QVector<A> _list;
};
Run Code Online (Sandbox Code Playgroud)