Jump to content
  • Advertisement
Sign in to follow this  
rumpfi88

template and map::iterator dependent scope

This topic is 2272 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:


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";
/* ... */
}


Here my attributes of class Polynomial


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


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
Advertisement
Add typename before std::map<char, U>::iterator. Edited by SiCrane

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 :


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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!