简体   繁体   English

在另一个模板类构造函数中使用typdef模板时出错

[英]Error when using typdef template in another template class constructor

I have a little problem with templates. 我的模板有点问题。 Since it's easier to explain with code, here is my problem. 由于使用代码更容易解​​释,这是我的问题。

I have an interface class : 我有一个接口类:

template <typename T>
class IElemValidator
{
public:
    virtual bool validate(T val) const = 0 ;
    virtual ~IElemValidator(){};
};

and a typedef struct : 和一个typedef结构:

template <typename T>
struct vecValidators
{
    typedef boost::ptr_vector<IElemValidator<T>> Type;
};

I can use my typedef struct everywhere except in the parameters of another template classe like this : 我可以在任何地方使用我的typedef结构,除了在另一个模板classe的参数中,如下所示:

template <typename T>
class CTestMaybe
{
public:
    CTestMaybe(vecValidators<T>::Type* a_Validators);
};

When trying to compile, I have this error: 在尝试编译时,我有这个错误:

Error   2   error C2061: syntax error : identifier 'Type'

Of course, I can do this : 当然,我可以这样做:

template <typename T>
class CTestMaybe
{
private:
    typedef boost::ptr_vector<IElemValidator<T>> vecValidator;

public:
    CTestMaybe(vecValidator* a_Validators);


};

and it's working well but I'm kinda loosing the interest of my struct class. 并且它运行良好,但我有点失去了我的struct类的兴趣。

So, what I'm doing wrong ? 那么,我做错了什么? Is there a "correct" way to do what I want ? 有没有“正确”的方式来做我想要的事情?

Thanks. 谢谢。

You have to add typename : 你必须添加typename

template <typename T>
class CTestMaybe
{
public:
    CTestMaybe(typename vecValidators<T>::Type* a_Validators);
};

The type vecValidators<T>::Type is a dependent name (if I get the terminology right). 类型vecValidators<T>::Type是一个从属名称 (如果我得到了正确的术语)。 This means you have to put an extra typename there: 这意味着你必须在那里放一个额外的typename

CTestMaybe(typename vecValidators<T>::Type* a_Validators);

C ++在函数声明中编译了一个类型,但它看到了vecValidators<T>::Type*并且由于vecValidators是一个template它不知道Type是其中的类型,所以你必须使用它来说明编译器typename所以你应该将你的功能改为:

CTestMaybe(typename vecValidators<T>::Type* a_Validators);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM