Reputation: 3321
I was having problem declaring an iterator to traverse through a map and find values. I'm getting a error of "expected initializer before 'fibiter'".
map <int, int> fibHash;
int memoized_fib(int n)
{
map <int, int> iterator fibiter = fibHash.find(n); //ERROR HERE
if(fibiter != fibHash.end())
return *fibiter;
int fib_val;
if(n <= 1)
fib_val = 1;
else
fib_val = memoized_fib(n - 1) + memoized_fib(n - 2);
fibHash[n] = fib_val;
return fib_val;
}
int main()
[..]
Upvotes: 1
Views: 113
Reputation: 147046
You forgot to use the scope resolution operator, ::
. The compiler thinks you declared a map<int, int>
named iterator
, and therefore gets mighty confused when it finds fibiter
.
map<int, int>::iterator fibiter
is what you want
Upvotes: 4
Reputation: 12983
map <int, int> iterator
→ map <int, int>::iterator
iterator is a typedef defined within the class "map".
You can look that up on the implementation of the standard library shipped with GCC 4.6.3 in <bits/stl_map.h>
line 139, you have:
typedef typename _Rep_type::iterator iterator;
As the typedef belongs to the definition of the class, you should add the ":" so that the compiler knows where to find it.
Upvotes: 1