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.

darkpotpot
  • 1,321
  • 2
  • 11
  • 25
  • 1
    See http://stackoverflow.com/questions/610245/where-and-why-do-i-have-to-put-the-template-and-typename-keywords – hmjd Oct 19 '12 at 08:00

3 Answers3

2

You have to add typename:

template <typename T>
class CTestMaybe
{
public:
    CTestMaybe(typename vecValidators<T>::Type* a_Validators);
};
nosid
  • 48,932
  • 13
  • 112
  • 139
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);
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
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);
BigBoss
  • 6,904
  • 2
  • 23
  • 38