Sign in to follow this  
Mawww

C++ template problem

Recommended Posts

g++ have trouble with that : std::vector< TemplatedStructure<T> >::const_iterator iter; expected ; before iter... I'm new to templates but I have not found any thing about this... whats the problem ?

Share this post


Link to post
Share on other sites
Looks fine on it's own. What version of g++ are you using? Also post a couple of the lines before that line as well as the declaration of TemplatedStructure.

Share this post


Link to post
Share on other sites

template<typename T>
struct mwCacheItem
{
T* item;
std::string name;

mwCacheItem (T* theItem, std::string theName)
: item(theItem), name(theName) {}

~mwCacheItem () { if (item) delete item; }
};

template<typename T>
class mwCacheTemplate
{
private:
std::vector< mwCacheItem<T> > itemList;

public:
T* query (std::string name)
{
std::vector< mwCacheItem<T> >::const_iterator iter;
for (iter
= itemList.begin(); iter != itemList.end(); iter++)
{
if ((*iter).name == name)
return (*iter).item;
}
...


that's the relevent part i think

Share this post


Link to post
Share on other sites
Assuming T is a yet-unspecified template parameter, std::vector< TemplatedStructure<T> >::const_iterator is a dependent name (it is a member of a class template which hasn't been fully specialized yet). C++ compilers assumes that a dependent name refers to a static variable unless you use the keyword typename.

typename std::vector< TemplatedStructure<T> >::const_iterator iter;

Share this post


Link to post
Share on other sites
Well? Don't you want to specify a valid template parameter?

Id est:
std::vector< TemplatedStructure<T> >::const_iterator iter;
Turns into:
std::vector< TemplatedStructure<int> >::const_iterator iter;

T is not a type. (edit: as fruny touches upon, at least not typically)


jfl.


[edit] And beaten... Oh well. Besides, fruny's reply is better.

Share this post


Link to post
Share on other sites
The reason it is so is because you could very well do:

template<class T> class foo
{
typedef T f;
};

template<> class foo<int>
{
static int f;
};


If I write foo<double>::f, the compiler can go and check what f is in foo<double> - a type. But if I write:
template<class T> class bar
{
foo<T>::bar b;
};


the compiler cannot check if the syntax is correct without deciding if foo<T>::bar should be a static variable (in which case the syntax is incorrect) or a nested type (in which case it is correct). The default choice is to treat it as a static variable. The addition of typename causes the compiler to treat it as a type.

template<class T> class bar
{
typename foo<T>::bar b;
};



Templates are syntax-checked where they are defined, but checks that depend on actually knowing the template pararameters (like, is a<b legal when a and b are of type quux) are obviously deferred until the point at which the template is used (which means that your program may compile even though some functions have errors if you don't actually call those functions).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this