我正在尝试打印容器,例如集合和 map 。我正在使用的书说以下代码是有效的:
#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;
}
但是我从 Visual Studio 的编译器收到错误。 我错过了什么?我知道这可能是一些简单的东西,比如包含,但我不熟悉 STL 容器和 C++ 迭代器。
我已经有 #include <iterator>
错误:
- “Container”:后跟“::”时必须是类或命名空间
- 'itr':未声明的标识符
- “const_iterator”:不是“全局命名空间”的成员
我确信还有一些是第一个的结果。
编辑:
根据教科书,以下代码应与我的 main.c 中的代码等效。我无法让它工作,但它可能会有所帮助:
ifstream fin("Test.txt");
string x;
set<string> s;
while( fin >> x)
s.insert(x);
编辑:
Visual Studio 构建输出:
------ 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 ==========
最佳答案
它认为这是一个函数声明:
set<string> s( istream_iterator<string>(fin),
istream_iterator<string>() );
添加一对额外的括号可以纠正它:
set<string> s( (istream_iterator<string>(fin)),
istream_iterator<string>() );
如果您搜索Most Vexing Parse,那么在SO上有很多这样的例子。 .
编辑:您还需要添加 #include <string>
关于C++打印容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10164112/