“不带模板参数使用” [英] "used without template parameters"

查看:90
本文介绍了“不带模板参数使用”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道之前也曾问过类似的问题,但是我读了其中的几个问题,但仍然看不到我要去哪里。当我只编写类而不将原型与定义分开时,一切正常。当我如下图所示分离原型和定义时,就会发生问题:

I realize similar questions have been asked before, but I read a couple of those and still don't see where I'm going wrong. When I simply write my class without separating the prototype from the definition, everything works fine. The problem happens when I separate the prototype and definition as shown below:

template<class T> class VisitedSet { 
public:
    VisitedSet(); 
    int getSize(); 
    void addSolution(const T& soln); 
    void evaluate(); 
private:
    vector<T> vec;
    int iteration;
};

作为给出此错误的定义的示例:

And as an example of a definition that gives me this error:

int VisitedSet::getSize() {
    return vec.size();

我以前从未做过模板化的类,所以如果这里的问题很琐碎,请原谅我。

I've never made a templated class before, so please pardon me if the problem here is trivial.

推荐答案

VisitedSet 是模板,而不是类,因此您可以请勿在嵌套名称说明符(例如 VisitedSet :: getSize())中使用 VisitedSet 。正如您为所有类T 指定类VisitedSet< T> 的声明一样,您必须指定 VisitedSet< T> :: getSize()用于所有类T

VisitedSet is a template, not a class, so you can’t use VisitedSet in a nested name specifier such as VisitedSet::getSize(). Just as you specified the declaration of class VisitedSet<T> for all class T, you must specify the definition of VisitedSet<T>::getSize() for all class T:

template<class T>
int VisitedSet<T>::getSize() {
//            ^^^
    return vec.size();
}

但是,可以像使用模板名称一样使用模板的名称模板定义中的类。

The name of a template can, however, be used as though it were a class within a template definition:

template<class T>
struct Example {
    Example* parent;
    T x, y;
};

在这种情况下, Example 的缩写 Example< T>

In this case, Example is short for Example<T>.

这篇关于“不带模板参数使用”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆