c++ - 重载++ 运算符以递增迭代器

标签 c++ visual-studio-2010 iterator operators operator-overloading

我当前正在创建一个方形列表,要求之一是重载前置和后缀++ 运算符。

我尝试重载 .hpp 文件中的++ 运算符以增加迭代器。但是当调用++ 运算符时,它不会调用重载代码,而只是使用默认值。

iterator& operator ++ () {  // pre-increment 
        std::list<int>::iterator i = list_Values.begin(); 
        advance(i,1); 
        return &*i; 
    } 

    iterator operator ++ (int) { // post-increment 
        std::list<int>::iterator i = list_Values.begin(); 
        advance(i,1); 
        return &*i; 
    } 

我尝试以这种方式调用++ 运算符:

int* test = sqrList.begin();
  ++test;

  test++;

最佳答案

int* test = sqrList.begin();
++test;
test++;

您正在递增 int*,并且由于没有 int* 类可供扩展,因此您不可能实际为以下对象创建运算符这种类型。

改为创建正确类型的运算符。您可能想创建自己的迭代器类型。

关于c++ - 重载++ 运算符以递增迭代器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9386266/

相关文章:

c++ - 为什么我不能在 map 中放置一个迭代器?

java - 使用 for 语句和 while 语句向前移动迭代器之间的区别

visual-studio-2008 - VS2008 -> VS2010 导致神秘的 STL 错误

c++ - 这段代码会使那些 vector 对齐吗?

c++ - 是否可以制作一个结构节点类型的数组?

c++ - 如何处理与 C++ 中的 unicode 支持相关的代码重复

c++ - 将 Mat 转换为 QPixmap

.net - 无法调试 Windows 服务 - 当前不会命中断点

python - 如何使用 SWIG 使 C++ 类可从 Python 迭代?

c++ - 在 Fedora 14 上编译 C++ 程序时出现编译错误