2

我正在尝试打印容器,例如集合和地图。我正在使用的书说以下代码是有效的:

#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”:不是“全局命名空间”的成员

还有一些我敢肯定是第一个的结果。

编辑:

根据教科书,以下代码应该与我的主要代码相同。我无法让它工作,但它可能会有所帮助:

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 ==========
4

2 回答 2

2

它认为这是一个函数声明:

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>

于 2012-04-15T18:09:15.460 回答
0

似乎编译器(也是clang ++)似乎感到困惑。不过,以下内容对我有用:

#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");                                               
    istream_iterator<string> first(fin), last;                              
    set<string> s(first, last);                                             
    print(s);                                                               

    return 0;                                                               
}

但是,我建议将您的print函数转换为接受您使用输出的迭代器范围的东西std::copy,如下所示:http ://www.sgi.com/tech/stl/ostream_iterator.html

于 2012-04-15T18:13:21.597 回答