C++ 扩展初始化列表仅适用于

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21579654/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:42:38  来源:igfitidea点击:

extended initializer lists only available with

c++arraysloopsif-statement

提问by Christian Gardner

I'm very new to C++ and I'm having trouble reading my errors I was able to eliminate most of them but I'm down to a few and I'm request help on them please.

我对 C++ 很陌生,我在阅读错误时遇到了麻烦,我能够消除其中的大部分错误,但我只剩下少数几个,请寻求帮助。

Here is the program

这是程序

#include <string>
#include <iostream>
using namespace std;
int main(){
 int *bN = new int[9];
 string bankNum;
 int *number = new int[9];
 int total, remain;
 int *multi = new int{7,3,9,7,3,9,7,3};
 cout<<"Please enter the bank number located at the bottom of the check"<<endl;
 cin>>bankNum;
 for(int i = 0; i < 8; i++){
  bN[i]= (bankNum[i]-48);
 }
 for(int i = 0; i < 8;i++){
  cout<<bN[i];
 }
 cout<<endl;
 for(int i = 0; i < 8;i++){
  cout<<multi[i];
 }
 cout<<endl;
 for(int i = 0; i < 8;i++){
  bN[i] = bN[i] * multi[i];
  cout<< bN[i];
 }
 cout<<endl;
 for(int i = 0; i < 8;i++){
  total += bN[i]
  cout<<total;
 }
 cout<<endl;
 remain = total % 10;
 if(remain == (bankNum[9] - 48)){
  cout<<"The Number is valad"<<endl;
  cout<<remain<<endl;
 }
}

and the errors

和错误

wm018@cs:~$ c++ bankNum.cpp
bankNum.cpp: In function aint main()a:
bankNum.cpp:9:19: warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x [enabled by default]
bankNum.cpp:9:38: error: cannot convert a<brace-enclosed initializer list>a to ainta in initialization
bankNum.cpp:30:3: error: expected a;a before acouta

回答by Mike Seymour

This style of initialisation, using braces:

这种初始化风格,使用大括号:

int *multi = new int{7,3,9,7,3,9,7,3};

was introduced to the language in 2011. Older compilers don't support it; some newer ones (like yours) only support it if you tell them; for your compiler:

于 2011 年引入该语言。较旧的编译器不支持它;一些较新的(比如你的)只有在你告诉他们时才会支持它;对于您的编译器:

c++ -std=c++0x bankNum.cpp

However, this form of initialisation still isn't valid for arrays created with new. Since it's small and only used locally, you could declare a local array; this doesn't need C++11 support:

但是,这种形式的初始化对于使用new. 由于它很小而且只在本地使用,你可以声明一个本地数组;这不需要 C++11 支持:

int multi[] = {7,3,9,7,3,9,7,3};

This also has the advantage of fixing the memory leak - if you use newto allocate memory, then you should free it with deletewhen you've finished with it.

这也有修复内存泄漏的优点——如果你new用来分配内存,那么你应该delete在完成后释放它。

If you did need dynamic allocation, you should use std::vectorto allocate and free the memory for you:

如果确实需要动态分配,则应使用std::vector为您分配和释放内存:

std::vector<int> multi {7,3,9,7,3,9,7,3};

Beware that your version of GCC is quite old, and has incomplete support for C++11.

请注意,您的 GCC 版本很旧,并且对 C++11 的支持不完整。