Why it says 'push_back' has not been declared?
Solution 1
v[0]
is a reference to the initial element in the vector
; it isn't the vector
itself. The element is of type int
, which is not a class type object and therefore has no member functions.
Are you looking for v.push_back(0);
?
Note that vector<int> v(30);
creates the vector
with 30 elements in it, each with a value of zero. Calling v.push_back(0);
will increase the size of the vector
to 31. This may or may not be the behavior your want; if it isn't, you'll need to clarify what, exactly, you are trying to do.
Solution 2
You need to do v.push_back(0)
as push_back
is the method of the vector not its element.
Solution 3
use v.push_back(0)
as v[0]
is an int
and not a vector
.
Solution 4
You have the wrong type.
v
is of type Vector. v[0]
is NOT a vector, rather, it is a reference to the first element (which will be an int
).
As a result, v[0]
does not have a push_back
method.
Only the vector itself (v
) has the method.
Solution 5
Just use v.push_back(0);
You have to push_back into a vector. Not into a specific element of a vector.
Related videos on Youtube
Comments
-
Rashid almost 2 years
Why it says 'push_back' has not been declared ?
#include <iostream> #include <vector> using namespace std; int main() { vector <int> v(30); v[0].push_back(0); return 0; }
-
abelenky about 13 yearsPlease provide the complete, exact error message, including any line numbers it refers to.
-