向量 c++ 98 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36173078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Vector c++ 98 error
提问by Harry the hacker
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:
所以我一直在阅读多本(并重新发送)C++ 书籍并学习向量,他们都告诉我定义一个这样的向量:
vector<int> v1 = {4 ,3 ,5};
however when i compile it (Im using gnu gcc compiler in codeblocks) it comes up with this error
但是当我编译它时(我在代码块中使用 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'
在 C++ 98 中,'v1' 必须由构造函数初始化,而不是由 '{...}'并且我还在下面得到另一个 sais: 无法从 '括号内的初始化列表' 转换 '{4, 3, 5}'到'std::vector v1'
if you could help me it'd be much appreciated. And i did include the vector library.
如果你能帮助我,我将不胜感激。我确实包含了矢量库。
回答by Mohit Jain
Initialization used by you is called initializer list
and it is supported c++11onwards.
调用您使用的初始化,initializer list
并且支持c++11。
To ensure code is compiled, use C++11
or later -std
option. Or in general, don't use C++98
.
为确保编译代码,请使用C++11
或稍后-std
选项。或者一般来说,不要使用C++98
.
If you are using g++, please read: Compiling C++11 with g++
如果您使用的是 g++,请阅读:使用 g++编译 C++11
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?)
根据评论 OP 正在使用代码块。在点击编译按钮之前,您可以使用以下步骤:(来源:如何向 Code::Blocks 编译器添加 C++11 支持?)
- 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
- 转到工具栏 -> 设置 -> 编译器
- 在“Selected compiler”下拉菜单中,确保选择“GNU GCC Compiler”
- 在其下方,选择“编译器设置”选项卡,然后选择下方的“编译器标志”选项卡
- 在下面的列表中,确保选中“让 g++ 遵循 C++11 ISO C++ 语言标准 [-std=c++11]”框
- 点击确定保存
回答by Vlad from Moscow
The C++98 Standard does not support initializer lists to initialize standard containers.
C++98 标准不支持初始化列表来初始化标准容器。
Try to set appropriate compiler options to compile the code according to the C++ 2011 Standard.
尝试设置适当的编译器选项以根据 C++ 2011 标准编译代码。
Another approach is to add elements to the vector individually like
另一种方法是将元素单独添加到向量中,例如
std::vector<int> v1;
v1.reserve( 3 );
v1.push_back( 4 );
v1.push_back( 3 );
v1.push_back( 5 );
Instead of the member function push_back
you can use overloaded operator +=
. For example
push_back
您可以使用重载 operator代替成员函数+=
。例如
std::vector<int> v1;
v1.reserve( 3 );
v1 += 4;
v1 += 3;
v1 += 5;
Or to use an array like
或者使用像这样的数组
const size_t N = 3;
int a[N] = { 4, 3, 5 };
std::vector<int> v1( a, a + N );
回答by Owl
Compile with the -std=c++11 compiler option at the end of the line in the makefile.
使用 makefile 中行尾的 -std=c++11 编译器选项进行编译。
So for example:
例如:
g++ -ggdb -O0 -c ENiX_Chocky.cpp -std=c++11
g++ -ggdb -O0 -c ENiX_NLPTest.cpp -std=c++11
...
Then when you link, use the -std=c++11 option again:
然后当你链接时,再次使用 -std=c++11 选项:
g++ -ggdb -O0 ENiX_Chocky.cpp ENiX_NLPTest.cpp -o CLINLPTest.cpp -std=c++11
The error will immediately disappear.
错误将立即消失。
回答by rahul agarwal
Follow these steps if you are using Codeblocks:
如果您使用的是代码块,请按照以下步骤操作:
1.Go to Toolbar -> Settings -> Compiler
1.转到工具栏->设置->编译器
2.In the "Selected compiler" drop-down menu, make sure "GNU GCC Compiler" is selected.
2.在“Selected compiler”下拉菜单中,确保选择了“GNU GCC Compiler”。
3.Below that, select the "compiler settings" tab and then the "compiler flags" tab underneath.
3.在其下方,选择“编译器设置”选项卡,然后选择下方的“编译器标志”选项卡。
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.
4.在下面的列表中,确保选中“Have g++ follow the C++11 ISO C++ language standard [-std=c++11]”框。
5.Click OK to save
5.点击确定保存
回答by Mukesh Kumar
Intilizer_list only supported on c++11 onwards.
Intilizer_list 仅在 c++11 以后支持。
Compile your program as below :
编译你的程序如下:
- If you are using c++11 features then follow
-std=c++11
option; - If you are using c++14 features then follow
-std=c++14
option.
- 如果您使用的是 c++11 功能,请遵循
-std=c++11
选项; - 如果您使用的是 c++14 功能,请遵循
-std=c++14
选项。
EX:
前任:
gcc Temp.cpp -std=c++11 -o out_put
gcc Temp.cpp -std=c++14 -o out_put