samedi 4 novembre 2017

Vector Iterator Not Incrementable but Should Be?

I have written code for an evolving neural network and one of my classes has the following member function:

bool Brain::makeLayer(int pos)
{
    int preSize = layers.size();
    std::vector<std::vector<Cell*>>::iterator iter;

    if (pos > layers.size()) {
        layers.push_back(std::vector<Cell*>());
        return layers.size() > preSize;
    }

    if (layers.size() == layers.capacity()) {
        layers.reserve(layers.size() + 1);
    }

    iter = layers.begin();

    if (pos == 1) {
        consoleOut((++iter == layers.end()) ? "True" : "false");
    }

    for (int i = 0; i < pos; i++) {
        iter++;
    }

    layers.insert(iter, std::vector<Cell*>());
    return layers.size() > preSize;
 }

where layers is defined as std::vector<std::vector<Cell*>> layers

I am getting an error vector iterator not incrementable on the line where I increment my iterator iter using ++iter.

Through some debugging, the size of layers is 1, it has a capacity of 2 and ther iterator is valid for layers.begin() but as soon as I try to increment it in any way it causes an error.

I have tried testing with different values of pos, and if called directly makeLayer(1) works as intended...

I have done tests and having a std::vector<T> vec(1) being of size 1, having an iterator iter equal to vec.begin(), not invalidated through some other member function, such that the statement ++iter == vec.end() always evaluates to true as long as the system has enough available memory and the vector has the available capacity, so why does it not?

Aucun commentaire:

Enregistrer un commentaire