I need Template/iterator Help


Help: I can't get this to compile with out an error message. It gets stuck on
set<T>::const_iterator i;

If I change to set<double>::const_iterator i; it compiles fine.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <set>
#include <string>
#include <iostream>

template <class T> void print(const set<T>& myset, const string& name)
{
cout << endl << name << ", size of set " << myset.size() << "\n[";
set<T>::const_iterator i;
for (i = myset.begin(); i !=myset.end(); ++i)
{
    cout << (*i) << ", " ;
}
cout << "]\n";
}


The error messages:
In function `void print(const std::set<T, std::less<_Key>, std::allocator<_CharT> >&, const std::string&)':
expected `;' before "i"
`i' undeclared (first use this function)
(Each undeclared identifier is reported only once for each function it appears in.)
Hi nothing wrong in your code.
just missing a:
1
2
3
4
#include <set>
#include <string>
#include <iostream>
using namespace std;

or use std:: with all the std stuff like:
 
std::cout << std::endl << name << ", size of set " << myset.size() << "\n[";


OR
1
2
3
4
5
6
7
#include <set>
#include <string>
#include <iostream>
using std::set;
using std::cout;
using std::endl;
using std::string;

What compiler are you using?
Last edited on
hello,
Jeff HS. I was using: using namespace std; in my code, but forgot to include it with my post. Sorry about that. So thats not the problem. I tried your other suggestions, but they did not work either. I got the same problem if I use vector instead of set also. The compiler i am using is Dev-C++ version 4.9.9.2 download from www.bloodshed.net.
Oh it comes with the MingW port of the GCC compiler.
Try updating it if you cant, tell me the version.
My GCC compiler on my Linux works just fine with the stl and self made templates, so does my Microsoft compiler if you are in a hurry consider using a Microsoft compiler.
You can download the windows sdk, it comes with the latest version or download the Visual studio express.

I was just showing you some ways to use the std namespace.

Jeff
Last edited on
Hello,
I down loaded the visual studio express. And it worked there. Thanks. Its seems that this visual studio express is a lot more complicated to learn then the simple Dev-C++ compiler I downloaded (thanks Mr. Gates).
Topic archived. No new replies allowed.