1

I am confused about why am I getting this error when I am pushing elements from outside of the main function though it works perfectly if I push it from the inside.

#include<iostream>
#include<vector>
using namespace std;

vector<int> vect;
vect.push_back(1);

int main(){
    return 0;
}

Output:

main.cpp:6:1: error: ‘vect’ does not name a type
 vect.push_back(1);
 ^~~~

I think I have included std namespace so it shouldn't throw an error. Could somebody please help me out?

  • 2
    You can't put arbitrary statements at the top level in C++. This sort of logic should be inside a function. If it's intended to initialize, you could provide an initializer for `vect` instead. – Jeremy Roman Jun 01 '20 at 17:56
  • 3
    You can't just put code outside functions. – drescherjm Jun 01 '20 at 17:56

0 Answers0