Global vector c ++

Is it possible to have a vector as a C ++ global variable? Like this:

class system {...};
vector<system> systems;

when I try to compile this, I get an error message. The compiler that I use is gcc, and I compile it as C ++.

+3
source share
6 answers

Yes, that might like:

#include <vector>

class system{ ... };

std::vector<system> systems;

So, the global var vector is defined after the class system is defined. The vector should be included and don't forget std :: before vector (or using the std namespace).

Edit: I was just thinking about something. There is also a function called the system. Try a different class name.

+9
source

system() - c-stdlib, , , , .

- (System?) , plz.

+7

g++ 3.4.4 Cygwin, :

test.cpp: 8: : / 1 `template class std::vector '

test.cpp: 8: : , `system '

test.cpp: 8: : 2

test.cpp: 8: : ';'

, , :

vector<class system> systems

+3

, extern

+1

:

#include<iostream>
#include<vector>
using namespace std;
class system{
  // class members
 };

vector<system> v;

int main()
{
   //do something
}

It works fine in my g ++ compiler. I don't think there should be any problem defining the vector variable globally, but it is not recommended.

0
source

Error, as often, in windows.h! "system" is defined in "windows.h" or something in it. I believe this is a function for a system call.

0
source

Source: https://habr.com/ru/post/1725415/


All Articles