C++中模板内的模板化对象

3

我正在尝试使用模板创建一个按区域排序的图像列表。此处定义的类在同一文件中的其余部分中有其实现。

template <typename RegionType>
class SortedType
{
    public:
        SortedType();
        ~SortedType();
        void makeEmpty();
        bool isFull() const;
        int lengthIs() const;
        void retrieveItem( RegionType&, bool& );
        void insertItem( RegionType  );
        void deleteItem( RegionType  );
        void resetList();
        bool isLastItem() const;
        void getNextItem( RegionType& );

    private:
        int length;
        NodeType<RegionType> *listData;
        NodeType<RegionType> *currentPos;
}; 

节点结构的定义如下:

节点结构体定义:

template <typename DataType>
struct NodeType
{
   DataType info;
   NodeType<DataType> *next;
};

当我尝试编译代码时,出现错误:在我原型化一个使用SortedType类的函数所在的行上,出现错误:error: SortedType不是一种类型。我认为这与我正在使用的SortedType类和NodeType类的模板有关,导致某种问题,但我不确定如何解决它。
编辑:第一个错误出现的原型化函数是:
int computeComponents(ImageType &, ImageType &, SortedType &);

我使用SortedType类的所有函数原型中都出现了错误。NodeType在SortedType之前被声明。


2
展示你原型化一个使用SortedType类的函数的代码行;或者展示错误对应的代码行。 - Preet Kukreti
只要在SortedType之前声明NodeType,就可以了。 - Anycorn
1个回答

2
int computeComponents(ImageType &, ImageType &, SortedType &);

应该是

template <typename RegionType>
int computeComponents(ImageType &, ImageType &, SortedType< RegionType > &);

我不得不在函数的实现中添加模板指定,但一旦我这样做了,它就编译通过了!非常感谢! - Wenadin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接