mercredi 26 avril 2017

Why iterator increment with += operator causes the error?

Why advance of iterator with += operator cause a error?
I want to advance iterator with += operator but it raise a error.

Note that I am using macOS 10.12.4 and following compiler

li.cpp:

#include <list>
#include <iostream>
#include <string>
int main() {
    std::list<std::string> x;

    x.push_front("British");
    x.push_back("character");
    x.push_front("Coding is unco");
    x.push_back("Society");
    x.push_back("City Hole is Fuck'in place for me");
    auto iter = x.begin();
    iter += 3;
    //std::advance(iter, 3);
    x.erase(iter);

    for (auto &e: x) {
        std::cout << e << "\n";
    }
}

error:

li.cpp:13:10: error: no viable overloaded '+='
    iter += 3;
    ~~~~ ^  ~
1 error generated.

compile: clang++ -std=c++11 -o li li.cpp

compiler version:

Apple LLVM version 8.1.0 (clang-802.0.42)
Target: x86_64-apple-darwin16.5.0
Thread model: posix
InstalledDir: /Applications/http://ift.tt/1z8WHIF

Aucun commentaire:

Enregistrer un commentaire