仅适用于扩展初始化列表 [英] extended initializer lists only available with
问题描述
我很新的C ++和我无法读我的错误,我能够消除大部分人,但我到了几个,我对他们要求帮助,请。
下面是节目
的#include<串GT;
#包括LT&;&iostream的GT;
使用命名空间std;
诠释主(){
为int * bN的新= INT [9];
串BANKNUM;
为int *数= INT新[9];
INT总,依然存在;
为int *多= INT新{7,3,9,7,3,9,7,3};
COUT&所述;&下;请输入位于支票底部银行号码&下;其中p ENDL;
CIN>> BANKNUM;
的for(int i = 0; I< 8;我++){
bN的[I] =(BANKNUM [I] -48);
}
的for(int i = 0; I< 8;我++){
COUT<< bN的[I]
}
COUT<< ENDL;
的for(int i = 0; I< 8;我++){
COUT<<多[我]
}
COUT<< ENDL;
的for(int i = 0; I< 8;我++){
bN的[I] = bN的[I] *多[我]
COUT<< bN的[I]
}
COUT<< ENDL;
的for(int i = 0; I< 8;我++){
总+ = bN的[I]
COUT<<全;
}
COUT<< ENDL;
保持=总%10;
如果(保持==(BANKNUM [9] - 48)){
COUT<<的个数为valad<< ENDL;
COUT<<保持<< ENDL;
}
}
和错误
wm018 @ CS:〜$ C ++ bankNum.cpp
bankNum.cpp:在功能上是不是主要的()A:
bankNum.cpp:9:19:警告:扩展初始化列表仅适用于-std = C ++ 0x或-std = GNU ++ 0x中[默认启用]
bankNum.cpp:9:38:错误:无法A&LT转换,大括号内的初始化列表>将在初始化âintâ
bankNum.cpp:30:3:错误:预期;④âcoutâ前
初始化的这种风格,使用大括号:
为int *多= INT新{7,3,9,7,3,9,7,3};
被介绍给在2011年较早的编译器不支持它的语言;一些较新的(像你这样)只支持它,如果你告诉他们;你的编译器:
C ++ -std =的C ++ 0x bankNum.cpp
不过,这种形式的初始化仍然无效与创建数组新的
。由于它的小,只在本地使用,你可以声明本地阵列;这并不需要C ++ 11的支持:
INT多[] = {7,3,9,7,3,9,7,3};
这也有固定的内存泄漏的优势 - 如果你使用新
来分配内存,那么你应该用释放它删除
当你完成它。
如果您确实需要动态分配,你应该使用的std ::矢量
来分配和释放内存为您提供:
的std ::矢量<&INT GT;多{7,3,9,7,3,9,7,3};
要注意的是你的GCC的版本是很老了,对C ++ 11不完全的支持。
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屋!