所以我一直在阅读多本(并且很反感)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继续。
为确保代码被编译,请使用 C++11
或更高版本的 -std
选项。或者一般来说,不要使用 C++98
。
如果您使用的是 g++,请阅读:Compiling C++11 with g++
来自评论 OP 正在使用代码块。您可以在点击编译按钮之前使用以下步骤:(来源:How can I add C++11 support to Code::Blocks compiler?)
- Go to Toolbar -> Settings -> Compiler
- In the "Selected compiler" drop-down menu, make sure "GNU GCC Compiler" is selected
- Below that, select the "compiler settings" tab and then the "compiler flags" tab underneath
- In the list below, make sure the box for "Have g++ follow the C++11 ISO C++ language standard [-std=c++11]" is checked
- Click OK to save
关于c++ - vector C++ 98 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36173078/