1

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.

1

3 Answers 3

2

You have to add typename:

template <typename T>
class CTestMaybe
{
public:
    CTestMaybe(typename vecValidators<T>::Type* a_Validators);
};
1
  • Oh thanks. I knew there was something with typename but I couldn't find where to put it.
    – darkpotpot
    Oct 19, 2012 at 8:07
1

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);
1

C++ compiler a type in your function declaration, but instead it see vecValidators<T>::Type* and since vecValidators is a template it don't know that Type is a type inside of it, so you must say it to the compiler using typename so you should change your function to:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.