views:

636

answers:

6
+2  Q: 

global vector C++

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

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

when I try to compile this I get an error. The compiler I'm using is gcc and I'm compiling as C++.

+2  A: 

When I compile your code with g++ 3.4.4 under Cygwin I get the following errors:

test.cpp:8: error: type/value mismatch at argument 1 in template parameter list for `template class std::vector'

test.cpp:8: error: expected a type, got `system'

test.cpp:8: error: template argument 2 is invalid

test.cpp:8: error: invalid type in declaration before ';' token

The problem is your class name system, either change the name of the class or use:

vector<class system> systems

Trent
how does adding class helps?
aJ
remove the header <cstdlib> and check again.
Prasoon Saurav
The only header included was <vector>.
Trent
+1 to counteract the ridiculous down-votes. The keyword `class` can indeed be used to disambiguate a meaning of a symbol. For the potential down-voters: it is customary to leave a comment explaining the vote.
avakar
@avakar: I agree. +1 from me as well.
the_drow
+9  A: 

Yes that can like this:

#include <vector>

class system{ ... };

std::vector<system> systems;

So the vector global var is defined after the definition of the class system. Vector must be included and don't forget std:: before vector (or using namespace std).

Edit: I just thought of something. There is also a function called system. Try a different class name.

VDVLeon
If you try compiling this code with gcc you get an error. See my answer about the class name being "system".
Trent
It compiles for me using g++ 4.4.1 (after removing the ...). "system" is not a reserved word in C++, if that is what you are thinking about.
anon
@Neil, I testing with g++ 3.4.4 under Cygwin. I know system is not a reserved keyword but for some reason g++ was giving an error on it.
Trent
However, `system` is a function declared in `<stdlib.h>` or `<cstdlib>`. <i>(I have it listed in Section 19.2 of "C, A Reference Manual" by Harbison and Steele)</i>. I definitely would choose another name.
Thomas Matthews
+3  A: 

Do you mean this:

#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 a vector variable globally, but it is not recommended.

Prasoon Saurav
This does not compile in my g++ compiler under cygwin. The problem is with the class name 'system'. See my answer.
Trent
There is no problem with 'system' here. I think you might have included the header <cstdlib> and that's why the problem.
Prasoon Saurav
A: 

I bet you declared it in a header file without extern

Nemanja Trifunovic
Any explanation for the downvote?
Nemanja Trifunovic
I didn't down vote you. I must say that this was a valid guess without the error itself. No idea why someone would downvote because of that
the_drow
+7  A: 

system() is a c-stdlib function, hence possibly an already defined name, so you can't re-use it.

Re-name it to something else (System?) and post the full error message next time, plz.

Marcus Lindblom
being a standard library function name does not make it a reserved name. If he has not included <stdlib.h> he should not see "system" in the symbol table. This seems to be a problem with atleast some versions of g++. Changing the class name does help in these case - see my answer.
Trent
even though this is true, the error occurs only if you include, somehow, windows.h !!!!
PierreBdR
right.. reserved is bad wording on my part.. I've updated it.
Marcus Lindblom
A: 

The error is, as often, in windows.h ! "system" is defined in "windows.h" or something included in it. I suppose it's the function to make a system call.

PierreBdR