Sign in to follow this  

template and map::iterator dependent scope

This topic is 2054 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a class template "Polynomial" which uses coefficients to calculate polynomial equations. With function "eval(const std::map<char,'type'>) it calculates the equation if i.e. x = 3.

First I want to figure out if 'x' is defined (using iterators), but my GNU compiler tells me that a typename is missing. Here my code:

[CODE]
template<class U>
U eval(const std::map<char, U>& values)
{
bool var_match = false;
for(std::map<char, U>::iterator it = values.begin(); it != values.end(); it++)
{
if((*it).first == varname)
var_match = true;
}
if(!var_match) throw "Variable name mismatch";
/* ... */
}
[/CODE]

Here my attributes of class Polynomial

[CODE]
template<class T>
class Polynomial
{
private:
char varname;
std::vector<T> coefficients;
/* ... */
};
[/CODE]

Compiler-Error (at for(...;...;...) line):
error: need ‘typename’ before ‘std::map<char, U>::iterator’ because ‘std::map<char, U>’ is a dependent scope

You have any ideas how to deal with it?

lg rumpfi88 Edited by rumpfi88

Share this post


Link to post
Share on other sites
What SiCrane said, also note that you need to use a const_iterator since you are passing a const reference :

[CODE]
for(typename std::map<char, U>::const_iterator it = values.begin(); it != values.end(); it++)
[/CODE] Edited by _moagstar_

Share this post


Link to post
Share on other sites

This topic is 2054 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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