开发者

how to write contents for linked list to a file

I have a write function which im using to write the contents of a list to a file. The list contains only numbers.

list<int>::iterator pos;
    for (pos = listStorage.begin(); p开发者_运维问答os != listStorage.end(); ++pos)
    {
        out << *pos << endl;
    }
    return out;

I am getting an error on compilation;

error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::list<_Ty>::_Const_iterator<_Secure_validation>' (or there is no acceptable conversion)

Can anyone help? Thanks


I'm fairly certain this is a const issue. Is your listStorage object declared as const? If so, you need to declare your iterator as

const list<int>::iterator pos;


Rather than an explicit loop, I'd use an algorithm:

std::copy(listStorage.cbegin(), listStorage.cend(),
          std::ostream_iterator<int>(out, "\n"));

This will probably prevent the problem you're seeing, and incidentally clean up the code and almost certainly run faster as well (though the speedup with come from using "\n" instead of endl).


You might want to check this:

http://www.daniweb.com/software-development/cpp/threads/179828

it discusses the same issue as yours.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜