0

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);
}
TR3478
  • 25
  • 6

2 Answers2

6

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

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

Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • Thank you for the fast response. Im just beginning and that reminds me that I have to learn the basics more. – TR3478 Jul 17 '20 at 11:03
  • It is a common beginner trap.... So the introduction of uniform initialization (which has also its trap :-/) – Jarod42 Jul 17 '20 at 11:34
1

The vector declaration

std::vector<int>rock();

is actually a function declaration, that returns a vector.

That's not what you intended. Drop the ():

std::vector<int> rock;
P.P
  • 117,907
  • 20
  • 175
  • 238
  • Thank you for the fast response. Im just beginning and that reminds me that I have to learn the basics more. – TR3478 Jul 17 '20 at 11:03