0
template<class U, class W>
std::ostream& operator<<( std::ostream & out, const Alias<U, W> & A)
{
    out<<A.ItoS.size()<<std::endl;
    for (std::map<W, U>::const_iterator it = A.ItoS.begin(); it != A.ItoS.end(); it++)
        out<<it -> first<<" "<<it -> second<<std::endl;
    return out;
}

errors are on line :
for (std::map<W, U>::const_iterator it = A.ItoS.begin(); it != A.ItoS.end(); it++)

error: expected ';' before 'it'

error: 'it' was not declared in this scope

Other class functions dont't have any compilation problems.

While I was using std::string instead of U and int instead of W everything was fine. I am using Codeblocks 10 with MinGW compiler.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
lpp
  • 3
  • 1

1 Answers1

2

You forgot typename here:

  typename std::map<W, U>::const_iterator it = A.ItoS.begin();
//^^^^^^^

const_iterator is a dependent name, therefore typename is required here. For detail explanation, read this topic:

In C++11, you could simply write:

 auto it = A.ItoS.begin();

which is a relief to C++ programmers!

Community
  • 1
  • 1
Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • Thank you, I am rather new to C++ so why do I need to put typename there? – lpp Mar 25 '12 at 18:01
  • 1
    @lpp: Go through the link : [Where and why do I have to put the "template" and "typename" keywords?](http://stackoverflow.com/questions/610245/where-and-why-do-i-have-to-put-the-template-and-typename-keywords) – Nawaz Mar 25 '12 at 18:03
  • Now I have aditional problem using that class. In my simple program consisting of header, class and main program, when I try using that class members or functions I get error : undefined reference to `Alias::Exist(std::string const&) const' Before this change from non-template to template everything was working properly. Is there anything what I could have missed ? I defined class as Alias Al – lpp Mar 25 '12 at 18:10
  • @lpp: That is a different problem. It seems you've defined them in `.cpp` file. If so, then move them into `.h` file, because template definitions should reside in `.h` file itself. – Nawaz Mar 25 '12 at 18:16
  • I don't understand what I have to do. Everything is defined in .hpp file and only implemented in .cpp file. Below are my sources for this. Main file(.cpp) : http://pastebin.com/WKd3gksU Header(.hpp) : http://pastebin.com/yRPjzTyT Implementation (.cpp) : http://pastebin.com/S9eCbC0s – lpp Mar 25 '12 at 18:22
  • @lpp: Move them to `.h` file. The functions of a class template should be implemented in the `.h` file itself, not in `.cpp` file. There should not exist `.cpp` file. Just move all the code to `.h` file. – Nawaz Mar 25 '12 at 18:25
  • I've moved them and all functions work except functions related to stream (overloading operator << and >> ). For them I still have the same error. – lpp Mar 25 '12 at 18:32
  • @lpp: that possibly means, you've not overloaded `<<` and `>>` for the types you're working with. – Nawaz Mar 25 '12 at 18:34
  • So I need to explicitly overload every type for operators I work with? I will have to make std::ostream& operator<<( std::ostream & out, const Alias & A) std::ostream& operator<<( std::ostream & out, const Alias & A), etc. – lpp Mar 25 '12 at 18:35
  • @lpp: No. In that case, you don't have to. Since in your case, `W` and `U` are types for which there already exist `<<` and `>>`. There must be some other problem. – Nawaz Mar 25 '12 at 18:38