向量 c++ 98 错误 [英] Vector c++ 98 error

查看:33
本文介绍了向量 c++ 98 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我一直在阅读多本(并重新发送)C++ 书籍并学习向量,他们都告诉我像这样定义一个向量:

 vectorv1 = {4 ,3 ,5};

但是当我编译它时(我在代码块中使用 gnu gcc 编译器)它出现了这个错误

<块引用>

在 c++ 98 中,'v1' 必须由构造函数初始化,而不是由 '{...}'我还在下面得到另一个说:无法将{4, 3, 5}"从大括号括起来的初始化列表"转换为std::vector v1"

如果你能帮助我,我将不胜感激.我确实包含了矢量库.

解决方案

你使用的初始化叫做initializer list 并且支持 以后.

为了确保代码被编译,请使用 C++11 或更高版本的 -std 选项.或者一般来说,不要使用C++98.

如果您使用的是 g++,请阅读:使用 g++ 编译 C++11

<小时>

来自评论 OP 正在使用代码块.在点击编译按钮之前,您可以使用以下步骤:(来源:如何为 Code::Blocks 编译器添加 C++11 支持?)

<块引用>

  1. 转到工具栏 -> 设置 -> 编译器
  2. 在Selected compiler"下拉菜单中,确保选择了GNU GCC Compiler"
  3. 在其下方,选择编译器设置"选项卡,然后选择下方的编译器标志"选项卡
  4. 在下面的列表中,确保选中让 g++ 遵循 C++11 ISO C++ 语言标准 [-std=c++11]"框
  5. 点击确定保存

so ive been reading multiple (and resent) c++ books and learning about vectors and they all are telling me to define a vector like this:

 vector<int> v1 = {4 ,3 ,5}; 

however when i compile it (Im using gnu gcc compiler in codeblocks) it comes up with this error

in c++ 98 'v1' must be initialized by constructor not by '{...}' and i also get another one underneath that that sais: could not convert '{4, 3, 5}' from 'brace enclosed initializer list' to 'std::vector v1'

if you could help me it'd be much appreciated. And i did include the vector library.

解决方案

Initialization used by you is called initializer list and it is supported onwards.

To ensure code is compiled, use C++11 or later -std option. Or in general, don't use C++98.

If you are using g++, please read: Compiling C++11 with g++


From comments OP is using codeblocks. You can use the following steps before hitting the compile button: (Source: How can I add C++11 support to Code::Blocks compiler?)

  1. Go to Toolbar -> Settings -> Compiler
  2. In the "Selected compiler" drop-down menu, make sure "GNU GCC Compiler" is selected
  3. Below that, select the "compiler settings" tab and then the "compiler flags" tab underneath
  4. In the list below, make sure the box for "Have g++ follow the C++11 ISO C++ language standard [-std=c++11]" is checked
  5. Click OK to save

这篇关于向量 c++ 98 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆