简体   繁体   中英

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 :

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 :

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.

So, what I'm doing wrong ? Is there a "correct" way to do what I want ?

Thanks.

You have to add 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). This means you have to put an extra typename there:

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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