缺少类型说明符 - C++ 中假定的 Int

发布于 2024-10-27 08:38:01 字数 714 浏览 1 评论 0原文

其中第二行抛出此错误,但我不确定为什么。

std::vector<std::string> weapons(100);
weapons[3] = "Rusty dagger";

--

这是我的整个文件:

//global variables
#ifndef _GLOBAL_
#define _GLOBAL_

#include <vector>
#include <iostream>
#include <string>

//prototypes
void NPCTalk(std::string const& speaker,std::vector<std::string> const& text);
void wait(double seconds);
void regionChange(int amount);
int getPositionInStringVector(std::vector<std::string> const& vec,std::string value);

//variables
std::vector<std::string> weapons(100);
weapons[3] = "Rusty dagger";

//defines
#define RegionChange 3

#endif //__GLOBAL__

These second line of these is throwing this error, but I'm not sure why.

std::vector<std::string> weapons(100);
weapons[3] = "Rusty dagger";

--

Here's my entire file:

//global variables
#ifndef _GLOBAL_
#define _GLOBAL_

#include <vector>
#include <iostream>
#include <string>

//prototypes
void NPCTalk(std::string const& speaker,std::vector<std::string> const& text);
void wait(double seconds);
void regionChange(int amount);
int getPositionInStringVector(std::vector<std::string> const& vec,std::string value);

//variables
std::vector<std::string> weapons(100);
weapons[3] = "Rusty dagger";

//defines
#define RegionChange 3

#endif //__GLOBAL__

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

﹏半生如梦愿梦如真 2024-11-03 08:38:01
weapons[3] = "Rusty dagger";

这是一个声明。您不能在全局范围内编写语句。您必须将其放入函数中。

weapons[3] = "Rusty dagger";

This is a statement. You can't write statements in global scope. You must put it inside a function.

给我一枪 2024-11-03 08:38:01
weapons[3] = "Rusty dagger";

是一个声明。它不能出现在函数之外。

相反,您可以将其放在 main() 的开头附近,或者放在程序早期调用的某个 init_weapons() 函数中。

weapons[3] = "Rusty dagger";

is a statement. It cannot appear outside of a function.

Instead, you could put it near the start of main(), or in some init_weapons() function which gets called early in your program.

逆流 2024-11-03 08:38:01

虽然调用初始化函数是解决问题的一种方法,但我认为可能还有一种替代方法。您可以在 .cpp 文件中的某个位置执行以下操作:

static const char * const init_ary[] = {
   "Fred",
   "Barney",
   "Joe"
};

::std::vector< ::std::string> names(init_ary, init_ary+3);

然后您将在 .h 文件中包含此声明:

extern ::std::vector< ::std::string> names;

但实际上,避免全局变量是最好的主意。由于多种原因,它们是严重的坏消息,其中一些原因并不明显。例如,它们使您的代码更难测试。

将变量传递给每个函数以避免出现它们,这对“优雅”的设计来说似乎是值得的。事实上,如果你开始编写代码,我敢打赌你会开始看到一些模式,这些模式会让你重新思考设计的某些部分,最终你会得到比你从全局中获得的虚假“优雅”更好、更实际优雅的东西。基于变量的设计。

While calling an initialization function is one way to handle your problem, I thought an alternative might be in order. Here is something you can do in a .cpp file someplace:

static const char * const init_ary[] = {
   "Fred",
   "Barney",
   "Joe"
};

::std::vector< ::std::string> names(init_ary, init_ary+3);

Then you would have this declaration in your .h file:

extern ::std::vector< ::std::string> names;

But really, avoiding global variables is the best idea. They are seriously bad news for a whole host of reasons, some not immediately obvious. For example, they make your code tons harder to test.

Its well worth the seeming hit to an 'elegant' design to just pass around the variable to each and every function in order to avoid having them. In fact, if you start coding that up, I bet you start seeing patterns that cause you to rethink parts of your design and you'll end up with something even nicer and more actually elegant than the false 'elegance' you get from the global variable based design.

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