Vector error (does not name a type)

Ok, I entered the code as it was in the textbook I'm reading, but the compiler is giving me an error on the 2nd-4th line from the code below, saying 'v' does not name a type. Can someone tell me what is wrong here?

1
2
3
4
vector<int> v(3);   // vector of 3 integers
v[0] =5;
v[1]=7;
v[2]=9;
Last edited on
Post the entire code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
inline void keep_window_open() {char ch; cin>>ch;}
//  The above code uses the standard library directly, eliminating the need for the std_lib_facilities.h

vector<int> v(3);
v[0]=5;
v[1]=7;
v[2]=9;

int main ()
{
    cout<<v[0];
    }
You can't make assignments in global scope, only in function bodies.
Thank you. The book never pointed that out and I never even thought of that as a potential issue.
Topic archived. No new replies allowed.