Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ MSVC 2015-自定义迭代器的std::for_中的编译器错误(仅在调试时)_C++_Stl_Visual Studio 2015 - Fatal编程技术网

C++ MSVC 2015-自定义迭代器的std::for_中的编译器错误(仅在调试时)

C++ MSVC 2015-自定义迭代器的std::for_中的编译器错误(仅在调试时),c++,stl,visual-studio-2015,C++,Stl,Visual Studio 2015,我已经实现了一个自定义容器类及其迭代器,并尝试对其上的每个使用。VC6、在线cpp.sh和MSVC15中的一切都编译得很好,后者只有在发行版中才能编译:当调试中出现一系列错误时,我报告了代码的过于简化版本 struct Functor { void operator()(int&) {} } func; struct Container { typedef int value_type; struct iterator { bool operator!=(itera

我已经实现了一个自定义容器类及其迭代器,并尝试对其上的每个使用
。VC6、在线cpp.sh和MSVC15中的一切都编译得很好,后者只有在发行版中才能编译:当调试中出现一系列错误时,我报告了代码的过于简化版本

struct Functor {
  void operator()(int&) {}
} func;

struct Container {
  typedef int value_type;
  struct iterator {
    bool operator!=(iterator const&) { return true; }
    iterator& operator++() { return *this; }
    value_type& operator*() { return i; } //this is just to compile, not for real
    int i; //this is just to compile, not for real
  };
  iterator begin() { return iterator(); }
  iterator end() { return iterator(); }
};

#include <algorithm>
int main() {
  Container c;
  std::for_each(c.begin(), c.end(), func);  // compile errors
  return 0;
}
结构函子{ void运算符()(int&){} }func; 结构容器{ typedef int value_type; 结构迭代器{ 布尔运算符!=(迭代器常量&){return true;} 迭代器和运算符++(){return*this;} value_type&operator*(){return i;}//这只是为了编译,不是为了实际 int i;//这只是为了编译,不是为了真实 }; 迭代器begin(){返回迭代器();} 迭代器end(){return iterator();} }; #包括 int main(){ 容器c; std::for_each(c.begin(),c.end(),func);//编译错误 返回0; }
错误:

1>c:\program files\microsoft visual studio 14.0\vc\include\xutility(838): error C2672: '_Iter_cat': no matching overloaded function found
1>  c:\program files\microsoft visual studio 14.0\vc\include\algorithm(31): note: see reference to function template instantiation 'void std::_Debug_range_ptr<_InIt,_Fn1>(_InIt,_InIt,_Pty &,std::_Dbfile_t,std::_Dbline_t)' being compiled
1>          with
1>          [
1>              _InIt=Container::iterator,
1>              _Fn1=Functor,
1>              _Pty=Functor
1>          ]
1>  [...]: note: see reference to function template instantiation '_Fn1 std::for_each<Container::iterator,Functor>(_InIt,_InIt,_Fn1)' being compiled
1>          with
1>          [
1>              _Fn1=Functor,
1>              _InIt=Container::iterator
1>          ]
1>c:\program files\microsoft visual studio 14.0\vc\include\xutility(838): error C2893: Failed to specialize function template 'iterator_traits<_Iter>::iterator_category std::_Iter_cat(const _Iter &)'
1>  c:\program files\microsoft visual studio 14.0\vc\include\xutility(838): note: With the following template arguments:
1>  c:\program files\microsoft visual studio 14.0\vc\include\xutility(838): note: '_Iter=Container::iterator'
1>c:\program files\microsoft visual studio 14.0\vc\include\xutility(838): error C2672: '_Debug_range_ptr2': no matching overloaded function found
1>c:\program files\microsoft visual studio 14.0\vc\include\xutility(838): error C2780: 'void std::_Debug_range_ptr2(_RanIt,_RanIt,_Pty &,std::_Dbfile_t,std::_Dbline_t,std::random_access_iterator_tag)': expects 6 arguments - 5 provided
1>  c:\program files\microsoft visual studio 14.0\vc\include\xutility(820): note: see declaration of 'std::_Debug_range_ptr2'
1>c:\program files\microsoft visual studio 14.0\vc\include\xutility(838): error C2780: 'void std::_Debug_range_ptr2(_InIt,_InIt,_Pty &,std::_Dbfile_t,std::_Dbline_t,std::input_iterator_tag)': expects 6 arguments - 5 provided
1>  c:\program files\microsoft visual studio 14.0\vc\include\xutility(811): note: see declaration of 'std::_Debug_range_ptr2'
1>c:\program files\microsoft visual studio 14.0\vc\include\xutility(838):错误C2672:“\u Iter\u cat”:未找到匹配的重载函数
1> c:\program files\microsoft visual studio 14.0\vc\include\algorithm(31):注意:请参阅正在编译的函数模板实例化“void std::_Debug_range_ptr(_InIt,_InIt,_Pty&,std:_Dbfile_t,std:_Dbline_t)”
1> 与
1>          [
1> _InIt=容器::迭代器,
1> _Fn1=函子,
1> _Pty=函子
1>          ]
1> […]:注意:请参阅正在编译的函数模板实例化“\u Fn1 std::for_each(\u InIt,\u InIt,\u Fn1)”的参考
1> 与
1>          [
1> _Fn1=函子,
1> _InIt=容器::迭代器
1>          ]
1> c:\program files\microsoft visual studio 14.0\vc\include\xutility(838):错误C2893:未能专门化函数模板“迭代器特征::迭代器类别标准::_Iter_cat(const _Iter&)”
1> c:\program files\microsoft visual studio 14.0\vc\include\xutility(838):注意:使用以下模板参数:
1> c:\program files\microsoft visual studio 14.0\vc\include\xutility(838):注意:“\u Iter=Container::iterator”
1> c:\program files\microsoft visual studio 14.0\vc\include\xutility(838):错误C2672:“\u Debug\u range\u ptr2”:未找到匹配的重载函数
1> c:\program files\microsoft visual studio 14.0\vc\include\xutility(838):错误C2780:“void std:”u Debug_range_ptr2(_RanIt,_RanIt,_RanIt,_Pty&,std::”u Dbfile_t,std:“\u Dbline_t,std::random_access_iterator_tag)”:需要6个参数-提供5个参数
1> c:\program files\microsoft visual studio 14.0\vc\include\xutility(820):注意:请参见“std::\u Debug\u range\u ptr2”的声明
1> c:\program files\microsoft visual studio 14.0\vc\include\xutility(838):错误C2780:“void std::_Debug_range_ptr2(_InIt,_InIt,_Pty&,std:_Dbfile_t,std:_Dbline_t,std::input_iterator_tag)”:需要6个参数-提供5个参数
1> c:\program files\microsoft visual studio 14.0\vc\include\xutility(811):注意:请参见“std::\u Debug\u range\u ptr2”的声明

通常迭代器被定义为标准类
std::iterator
的派生类,其声明如下

template<class Category, class T, class Distance = ptrdiff_t,
class Pointer = T*, class Reference = T&> struct iterator;
模板结构迭代器;
它的模板参数之一是Category,它确定迭代器的Category标记,例如
std::input\u iterator\u tag

在调试模式下,编译器根据迭代器的类别执行一些检查


您应该遵循定义迭代器的标准模型。

MSVC在使用迭代器时有更广泛的调试检查。这可能需要对迭代器及其所有嵌入类型进行更完整的定义

传统上,迭代器是从中派生出来的,它为所需的嵌入式类型提供默认定义

template< 
    class Category,
    class T,
    class Distance = std::ptrdiff_t,
    class Pointer = T*,
    class Reference = T& 
> struct iterator;
模板<
类别,,
T类,
类距离=标准::ptrdiff\u t,
类指针=T*,
类参考=T&
>结构迭代器;
对代码进行如下更改应能澄清问题

  struct iterator : std::iterator<
    std::input_iterator_tag, // or as required
    int
  >
结构迭代器:std::迭代器< std::输入\迭代器\标记,//或根据需要 int >
我认为这个错误是可以自我解释的。阅读C++中的迭代器。他们必须提供一些接口,比如指定迭代器的“类型”(
iterator\u category
)就是这样,我不知道。迭代器的新(派生)定义解决了这个问题。谢谢大家。从C++17开始,
std::iterator
已被弃用。我们应该通过using简单地定义实际需要的类型。