Why it says 'push_back' has not been declared?

11,982

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.

Share:
11,982

Related videos on Youtube

Rashid
Author by

Rashid

...

Updated on June 04, 2022

Comments

  • Rashid
    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
      abelenky about 13 years
      Please provide the complete, exact error message, including any line numbers it refers to.