c++ - 无法分配列表迭代器 [C++]

标签 c++ visual-c++ stl

我有以下方法:

void* vpArr_t::operator[](int i) const
{
    if (!isEmpty() && i >= 0 && i < nOfItems)
    {
        list<void*>::iterator it;
        int idx;
        for(it = array.begin(), idx = 0; idx < i; ++it, ++idx); // go to the i'th element 
        return *it;
    }
    else
    {
        return NULL;
    }
}

地点:

array是列表类型。

我在以下行中得到红色下划线(编译错误):

for(it = array.begin(), idx = 0; idx < i; ++it, ++idx);

在:

it = array.begin()

它说我要设置一个 list<void*>::const_iterator进入list<void*>::iterator类型。

但我注意到有一个 iterator begin() 的重载方法。如何解决这个错误?我正在使用 Visual C++ 2012。

最佳答案

大概array是一个数据成员,而你在一个const成员函数中,所以你不能在数据成员上调用非const方法。所以你需要一个const_iterator:

list<void*>::const_iterator it,

关于c++ - 无法分配列表迭代器 [C++],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16714007/

相关文章:

java - JNI,从 C++ 调用 Java,CallObjectMethod 上的 SIGSEGV

c++ - C++应该如何执行PowerShell命令?

c++ - 在 C++ 中将 TCP 服务器数据包保存到文件

c++ - initializer_list后面 "hidden array"的存储

c++ - 使用 bind1st 和 mem_fun 引用成员函数

c++ - 如何使用无类型函数指针调用 C++ 成员函数

c++ - 如何在Cygwin GCC 4.7.2下使用C++ 11的std::thread

c++ - 在 Visual Studio 中通过 printf() 打印 std::string

c++ - 尝试在 hash_map 中查找记录时应用程序崩溃

c++ - 无效使用不完整类型 'class …' STL vector