C++ 为什么Xcode让我编译这个自定义迭代器代码?

C++ 为什么Xcode让我编译这个自定义迭代器代码?,c++,xcode,c++11,C++,Xcode,C++11,我正在学习自定义迭代器,并编写了以下玩具示例: #include <iostream> #include <iterator> #include <algorithm> bool IsOdd (int i) { return ((i%2)==1); } class MyIterator : std::iterator<std::forward_iterator_tag, int> { int *p; public: MyIterat

我正在学习自定义迭代器,并编写了以下玩具示例:

#include <iostream>
#include <iterator>
#include <algorithm>

bool IsOdd (int i) {
  return ((i%2)==1);
}

class MyIterator : std::iterator<std::forward_iterator_tag, int>
{
  int *p;
public:
  MyIterator(int* x) :p(x) {}
  MyIterator(const MyIterator& mit) : p(mit.p) {}
  MyIterator& operator++() {++p;return *this;}
  MyIterator operator++(int) {MyIterator tmp(*this); operator++(); return tmp;}
  bool operator==(const MyIterator& rhs) {return p==rhs.p;}
  bool operator!=(const MyIterator& rhs) {return p!=rhs.p;}
  int& operator*() {return *p;}
};


int main () {
  int numbers[]={2,3,4,5,6};
  MyIterator from(numbers);
  MyIterator until(numbers+5);

  MyIterator ii = std::find_if(from, until, IsOdd);
  std::cout << "The first odd value is " << *ii << '\n';

  return 0;
}
#包括
#包括
#包括
bool IsOdd(国际一级){
返回((i%2)==1);
}
类MyIterator:std::iterator
{
int*p;
公众:
MyIterator(int*x):p(x){}
MyIterator(constmyiterator&mit):p(mit.p){}
MyIterator&operator++(){++p;返回*this;}
MyIterator运算符++(int){MyIterator tmp(*this);运算符++();返回tmp;}
bool运算符==(constmyiterator&rhs){return p==rhs.p;}
布尔运算符!=(常量MyIterator&rhs){return p!=rhs.p;}
int&运算符*(){return*p;}
};
int main(){
整数[]={2,3,4,5,6};
来自(数字)的MyIterator;
MyIterator直到(数字+5);
MyIterator ii=std::find_if(从,直到,IsOdd);

std::cout如果您知道这个问题,为什么要私下继承?您自己没有使用任何提供的typedef…从形式上讲,您的程序显示出未定义的行为,因为
MyIterator
不满足迭代器要求(因为未能提供预期提供的某些公共typedef)。“似乎可以工作”是未定义行为的一种可能表现形式;“无法编译”是另一个。我不确定公开继承是否是消除错误的正确方法,但我会这样做。谢谢你的帮助!如果你知道这个问题,为什么你要私下继承?你自己没有使用任何提供的typedef…从形式上讲,你的程序表现出未定义的行为,因为
MyIterator
没有满足迭代器要求(对于未能提供预期提供的某些公共typedef)。“似乎有效”是未定义行为的一种可能表现形式;“未能编译”是另一种可能表现形式。我不确定公开继承是否是消除错误的正确方法,但我会这样做。感谢您的帮助!
In file included from iterators.cpp:5:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/5.3.0/../../../../include/c++/5.3.0/algorithm:62:
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.3.0/../../../../include/c++/5.3.0/bits/stl_algo.h:162:10: error: no matching function for call to '__iterator_category'
                       std::__iterator_category(__first));
                       ^~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.3.0/../../../../include/c++/5.3.0/bits/stl_algo.h:3790:19: note: in instantiation of function template specialization
      'std::__find_if<MyIterator, __gnu_cxx::__ops::_Iter_equals_val<const int> >' requested here
      return std::__find_if(__first, __last,
                  ^
iterators.cpp:37:24: note: in instantiation of function template specialization 'std::find<MyIterator, int>' requested here
  MyIterator ii = std::find(from, until, 31);
                       ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.3.0/../../../../include/c++/5.3.0/bits/stl_iterator_base_types.h:204:5: note: candidate template ignored: substitution failure
      [with _Iter = MyIterator]: no type named 'iterator_category' in 'std::iterator_traits<MyIterator>'
    __iterator_category(const _Iter&)
    ^
1 error generated.