扩展初始化列表仅可用 [英] extended initializer lists only available with

查看:221
本文介绍了扩展初始化列表仅可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C ++的新手,我无法读取我的错误,我能够消除大部分的,但我下了几个,我请求帮助他们。



这是程序

  #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<<请输入位于支票底部的银行号<< 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 <
}
cout<< endl;
remaining = total%10;
if(remain ==(bankNum [9] - 48)){
cout<<Number is valad<
cout<< remain<< endl;
}
}

和错误

  wm018 @ cs:〜$ c ++ bankNum.cpp 
bankNum.cpp:在函数中âintmain()â:
bankNum.cpp: 9:19:warning:扩展初始化器列表只能使用-std = c ++ 0x或-std = gnu ++ 0x [默认启用]
bankNum.cpp:9:38:error:can not convertâ<括号括起始化器列表>â到初始化中的âintâ€b $ b bankNum.cpp:30:3:错误:预期â;â前âcoutâ


解决方案

这种样式的初始化,使用大括号:

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

在2011年被引入该语言。旧的编译器不支持它;一些较新的(如你的)只支持它,如果你告诉他们; for your compiler:

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

但是,这种初始化形式对于使用 new 创建的数组无效。由于它很小,只能在本地使用,你可以声明一个局部数组;这不需要C ++ 11支持:

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

这也有固定内存泄漏的优点 - 如果你使用 new 以分配内存,那么您应该在完成后用 delete 将其释放。



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

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

请注意,您的GCC版本相当旧,并且对C ++ 11有不完整的支持。 / p>

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.

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 âint main()â:
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 â<brace-enclosed initializer list>â to âintâ in initialization
bankNum.cpp:30:3: error: expected â;â before âcoutâ

解决方案

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:

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:

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

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

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

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.

这篇关于扩展初始化列表仅可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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