I am attempting to print containers, like sets and maps. The book I am using says the following code is valid:
#include <iostream>
#include <set>
#include <iterator>
#include <fstream>
using namespace std;
template <typename Container>
void print (const Container & c, ostream & out = cout)
{
typename Container::const_iterator itr;
for( itr=c.begin(); itr!=c.end(); ++itr)
out << *itr << " ";
out << endl;
}
int main()
{
ifstream fin("Test.txt");
set<string> s( istream_iterator<string>(fin),
istream_iterator<string>() );
print( s );
return 0;
}
Yet I am getting the an error from Visual Studio's compiler. What am I missing? I know it is likely something simple like an include yet I am unfamiliar with STL Containers and C++ iterators.
I already have #include <iterator>
Errors:
- 'Container': must be a class or namespace when followed by '::'
- 'itr' : undeclared identifier
- 'const_iterator' : is not a member of '`global namespace''
and a few more I am sure are a result of the first one.
Edit:
Per the textbook, the following code should be equivalent to that in my main. I could not get it to work but it may help:
ifstream fin("Test.txt");
string x;
set<string> s;
while( fin >> x)
s.insert(x);
Edit:
Visual Studio build output:
------ Build started: Project: project, Configuration: Debug Win32 ------
Build started 4/15/2012 1:19:25 PM.
InitializeBuildStatus:
Touching "Debug\project.unsuccessfulbuild".
ClCompile:
Project4.cpp
c:\users\admin\documents\visual studio 2010\projects\project\project\project4.cpp(11): error C2825: 'Container': must be a class or namespace when followed by '::'
c:\users\admin\documents\visual studio 2010\projects\project\project\project4.cpp(22) : see reference to function template instantiation 'void print<std::set<_Kty>
(std::istream_iterator<_Ty>,std::istream_iterator<_Ty> (__cdecl *)(void))>(Container (__cdecl &),std::ostream &)' being compiled
with
[
_Kty=std::string,
_Ty=std::string,
Container=std::set<std::string> (std::istream_iterator<std::string>,std::istream_iterator<std::string> (__cdecl *)(void))
]
c:\users\admin\documents\visual studio 2010\projects\project\project\project4.cpp(11): error C2039: 'const_iterator' : is not a member of '`global namespace''
c:\users\admin\documents\visual studio 2010\projects\project\project\project4.cpp(11): error C2146: syntax error : missing ';' before identifier 'itr'
c:\users\admin\documents\visual studio 2010\projects\project\project\project4.cpp(11): error C2065: 'itr' : undeclared identifier
c:\users\admin\documents\visual studio 2010\projects\project\project\project4.cpp(12): error C2065: 'itr' : undeclared identifier
c:\users\admin\documents\visual studio 2010\projects\project\project\project4.cpp(12): error C2228: left of '.begin' must have class/struct/union
type is 'std::set<_Kty> (__cdecl &)'
with
[
_Kty=std::string
]
c:\users\admin\documents\visual studio 2010\projects\project\project\project4.cpp(12): error C2065: 'itr' : undeclared identifier
c:\users\admin\documents\visual studio 2010\projects\project\project\project4.cpp(12): error C2228: left of '.end' must have class/struct/union
type is 'std::set<_Kty> (__cdecl &)'
with
[
_Kty=std::string
]
c:\users\admin\documents\visual studio 2010\projects\project\project\project4.cpp(12): error C2065: 'itr' : undeclared identifier
c:\users\admin\documents\visual studio 2010\projects\project\project\project4.cpp(13): error C2065: 'itr' : undeclared identifier
Build FAILED.
Time Elapsed 00:00:01.00
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========