Search code examples
c++push-back

push_back iterator in vector?


I tried to store the number 1,4,7...97 into a vector. if I use std::out << i; the for loop is working.

but the push_back gives an error: base of member reference is a function perhaps you meant to call it with no arguments. i googled it and couldn't find a answer which helped me.

The only thing I found was this:

"In C++ adding elements to a vector may cause reallocation of the contained data, which will invalidate all iterators. That means you can't loop over the vector using iterators (which is what the range-based for loop does) while also inserting new elements. You can however iterate using indexes and use the vector size as condition, since indexes will always be the same."

but do I loop over the vector? I thought I just store the iterator

I appreciate every help

std::vector<int>rock();

int i;
for (int i = 1; i < 100; i+=3)
{
std::cout<< " " << i;
rock.push_back(i);
}

Solution

  • std::vector<int>rock(); is a .. function declaration.

    use std::vector<int>rock; or std::vector<int>rock{}; instead.