全局向量 C++
C++ 中是否可以将向量作为全局变量? 像这样:
class system {...};
vector<system> systems;
当我尝试编译它时,我收到错误。我使用的编译器是 gcc,我正在编译为 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++.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
是的,可以这样:
所以向量全局变量是在类系统定义之后定义的。必须包含向量,并且不要忘记向量之前的 std:: (或使用命名空间 std)。
编辑:
我刚刚想到一件事。还有一个函数叫system。尝试使用不同的类名。
Yes that can like this:
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.
system()
是一个 c-stdlib 函数,因此可能是一个已经定义的名称,因此您无法重复使用它。将其重命名为其他名称(系统?)并在下次发布完整的错误消息,请。
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.
当我在 Cygwin 下使用 g++ 3.4.4 编译代码时,出现以下错误:
问题是你的类名系统,要么更改类名,要么使用:
vector;系统
When I compile your code with g++ 3.4.4 under Cygwin I get the following errors:
The problem is your class name system, either change the name of the class or use:
vector<class system> systems
我敢打赌你在没有
extern
的头文件中声明了它I bet you declared it in a header file without
extern
您的意思是这样吗:
它在我的 g++ 编译器中运行良好。我认为全局定义向量变量应该不会有任何问题,但不建议这样做。
Do you mean this:
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.
错误通常出现在 windows.h 中! “system”在“windows.h”或其中包含的内容中定义。我想这是进行系统调用的函数。
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.