开发者

C++ templates inside templates [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Where and why do I have to put “template” and “typename” on dependent names?

Here's my problem:


template<typename TypeName> class Bubu
{
   ...
   vector<TypeName>::iterator Foo()
   {
      ...
   }
   ...
}

This gives:

error C2146: syntax error: missing ';' before identifier 'Foo'

If I change the typename to an actual type, like int or SomeClass, it works:


vector<int>::iterator Foo(){}

What I want to have is something like:


Bubu<SomeClassType> b开发者_JAVA技巧ubuInstance;
vector<SomeClassType>::iterator it = bubuInstance.Foo();

What's wrong? How do I fix it?



Search for "dependent names" on google.

Anyway to fix it use typename :

template<typename TypeName> class Bubu
{
   ...
   typename vector<TypeName>::iterator Foo()
   {
      ...
   }
   ...
}


You need to prefix 'typename', and also finish-off your class with a semi-colon ';'

template<typename TypeName>
class Bubu
{
    typename vector<typename TypeName>::iterator Foo()
    {
    }
};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜