全局向量 C++

发布于 2024-08-14 15:14:11 字数 155 浏览 3 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

不羁少年 2024-08-21 15:14:11

是的,可以这样:

#include <vector>

class system{ ... };

std::vector<system> systems;

所以向量全局变量是在类系统定义之后定义的。必须包含向量,并且不要忘记向量之前的 std:: (或使用命名空间 std)。

编辑:
我刚刚想到一件事。还有一个函数叫system。尝试使用不同的类名。

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.

蓝礼 2024-08-21 15:14:11

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.

毁梦 2024-08-21 15:14:11

当我在 Cygwin 下使用 g++ 3.4.4 编译代码时,出现以下错误:

test.cpp:8: 错误:模板参数列表中参数 1 的类型/值不匹配
对于“模板类 std::vector”

test.cpp:8:错误:需要一个类型,得到“系统”

test.cpp:8: 错误:模板参数 2 无效

test.cpp:8: 错误:“;”之前的声明中的类型无效令牌

问题是你的类名系统,要么更改类名,要么使用:

vector;系统

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

天煞孤星 2024-08-21 15:14:11

我敢打赌你在没有 extern 的头文件中声明了它

I bet you declared it in a header file without extern

离去的眼神 2024-08-21 15:14:11

您的意思是这样吗:

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

vector<system> v;

int main()
{
   //do something
}

它在我的 g++ 编译器中运行良好。我认为全局定义向量变量应该不会有任何问题,但不建议这样做。

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.

迷爱 2024-08-21 15:14:11

错误通常出现在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文