Error request for member 'push_back' in ... which is of non-class type 'int' when using std::vector
05:59 20 Jan 2022

I am learning how to use std::vector in C++ and I'm trying to create a vector of integers and add a value to it. However, my code fails to compile.

Here is a minimal, reproducible example:

#include 
#include 
using namespace std;

int main()
{
    vector prime(1);    
    prime[0].push_back(3);
    cout << prime[0];
}

When I try to compile this with g++, I get the following error:

Untitled-1.cpp:8:14: error: request for member 'push_back' in 'prime.std::vector::operator', which is of non-class type '__gnu_cxx::__alloc_traits::value_type' {aka 'int'} prime[0].push_back(3); ^~~~~~~~~

I don't understand the error. I thought prime[0] would allow me to modify the vector at that position. Why is the compiler saying the type is int and how can I correctly add the value 3 to the vector?

c++