简体   繁体   English

为什么会出现此模板编译错误?

[英]Why am i getting this template compile error?

Am learning C++ STL, and am trying out a small program but am getting some weird template error when compiling, my code looks like this; 我正在学习C ++ STL,正在尝试一个小程序,但是在编译时遇到了一些奇怪的模板错误,我的代码看起来像这样;

template<class InputIterator,  class OutputIterator, class Predicate>
OutputIterator remove_cpy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred)
{
    while (first != last){
        if (!pred(*first)) *result = *first;
        ++ first; 
    }

    return result;
}

bool is_manager(const Employee& emp){
    return emp.title == "manager";
}

struct Employee{
    std::string name;
    std::string title;
};

int main()
{
  vector<Employee> v;

  // create e1 to e4
  Employee e1;
  e1.name = "john";
  e1.title = "accountant";
  ...
  e4.title = "manager";

  // add e1 to e4 to vector
  v.push_back(e1);
  ...
  v.push_back(e4);

  remove_cpy_if(v.begin(), v.end(), std::ostream_iterator<Employee>(cout), is_manager);
  ....
}

I try compiling i get; 我尝试编译得到;

$ g++ -o test test.cpp -std=c++11 $ g ++ -o测试test.cpp -std = c ++ 11

In file included from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\iterator:66:0,
                 from test.cpp:5:
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stream_iterator.h: In instantiation of 'std::ostream_iterator<_Tp, _CharT, _Traits>& st
d::ostream_iterator<_Tp, _CharT, _Traits>::operator=(const _Tp&) [with _Tp = Employee; _CharT = char; _Traits = std::char_traits<char>]
':
test.cpp:19:30:   required from 'OutputIterator remove_cpy_if(InputIterator, InputIterator, OutputIterator, Predicate) [with InputItera
tor = __gnu_cxx::__normal_iterator<Employee*, std::vector<Employee> >; OutputIterator = std::ostream_iterator<Employee>; Predicate = bo
ol (*)(const Employee&)]'
test.cpp:78:95:   required from here
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stream_iterator.h:198:13: error: cannot bind 'std::ostream_iterator<Employee>::ostream_
type {aka std::basic_ostream<char>}' lvalue to 'std::basic_ostream<char>&&'
  *_M_stream << __value;
             ^
In file included from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\iostream:39:0,
                 from test.cpp:1:
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\ostream:602:5: error:   initializing argument 1 of 'std::basic_ostream<_CharT, _Traits>& std
::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = Employee]'

     operator<<(basic_ostream<_CharT, _Traits>&& __os, const _Tp& __x)
     ^

Compiling using Mingw g++ 4.8.1, Windows 7 SP1 使用Mingw g ++ 4.8.1,Windows 7 SP1进行编译

What am i missing? 我想念什么?

regards Gath. 问候盖特。

You never define how an Employee should be outputed. 您永远不会定义如何输出Employee
Just implement the following function and you will be good! 只需实现以下功能,您就会很好!

std::ostream&
operator<<(std::ostream& out, const Employee& e)
{
    /* Print an Employee. */
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我在尝试编译时遇到此编译错误? - Why am i getting this compile error when i try to compile? 为什么我遇到错误,“结构节点”需要模板参数? - Why I am getting error, template argument required for ‘struct node’? 为什么我收到错误“(指针名称)不是模板)? - why i am getting error " (name of the pointer ) is not a template )? 为什么我的模板函数指针出现链接器错误? - Why am I getting a Linker error with template function pointer? 为什么我收到错误“非模板&#39;f&#39;用作模板” - Why am I getting the error “non-template 'f' used as template” 为什么我收到编译错误“使用已删除的函数&#39;std :: unique_ptr ...” - Why am I getting compile error “use of deleted function 'std::unique_ptr …” 为什么在尝试使用 MSVC 编译 32 位代码时出现错误? - Why am I getting an error when trying to compile 32-bit code with MSVC? 为什么我会收到编译错误“ <variable> 没有在此范围内声明”? - Why am I getting compile errors “<variable> not declared in this scope”? 为什么我收到错误“参数包&#39;F&#39;必须位于模板参数列表的末尾” - Why am I getting the error “parameter pack 'F' must be at the end of the template parameter list” 为什么我会收到错误:当我尝试编译C ++代码时,在Eclipse中初始化&#39;Item :: Item(int)&#39;[-fpermissive]的参数1? - Why am I getting error: initializing argument 1 of 'Item::Item(int)' [-fpermissive] in Eclipse when I try to compile my C++ code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM