Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


template and map::iterator dependent scope


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 rumpfi88   Members   -  Reputation: 185

Like
0Likes
Like

Posted 02 May 2012 - 05:54 AM

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, 02 May 2012 - 05:55 AM.


Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9662

Like
0Likes
Like

Posted 02 May 2012 - 05:58 AM

Add typename before std::map<char, U>::iterator.

Edited by SiCrane, 02 May 2012 - 05:58 AM.


#3 _moagstar_   Members   -  Reputation: 465

Like
1Likes
Like

Posted 02 May 2012 - 06:02 AM

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_, 02 May 2012 - 06:03 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS