c++ - vector C++ 98 错误

标签 c++ vector

所以我一直在阅读多本(并且很反感)c++ 书籍并学习 vector ,他们都告诉我要像这样定义一个 vector :

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

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

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'

如果你能帮助我,我将不胜感激。 我确实包含了 vector 库。

最佳答案

您使用的初始化称为initializer list并且支持继续。

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

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


来自评论 OP 正在使用代码块。您可以在点击编译按钮之前使用以下步骤:(来源: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++ - vector C++ 98 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36173078/

相关文章:

c++ - 如何获取当前包含正在运行的 Windows/Linux 的磁盘号/名称?

C++:宏可以将 "abc"扩展为 'a' 、 'b' 、 'c' 吗?

r - 比较同一向量的相邻元素(避免循环)

algorithm - 对矢量场进行颜色编码

c++ - 打印 vector - 空输出

c++ - 将 std::vector<int> 保存到文件

c++ - 编译器如何知道函数调用后控制应该返回到哪里?

c++ - ComboBoxEx32 (CComboBoxEx) 键盘行为

c++ - COM 初始化和在 Win32 C++ DLL 中的使用

C++ 索引到索引映射