C++ char b[] 的初始值设定项过多
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21581095/
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
Too many initializers for char b[]
提问by Artiza Ali
It is morse code program.
I am getting error of too many initializers for char b[]
. How can I get rid of this error?
这是莫尔斯电码程序。
我收到错误too many initializers for char b[]
。我怎样才能摆脱这个错误?
#include<iostream>
using namespace std;
int main(){
char a[72]={'A','a','B','b','C','c','D','d','E','e','F','f','G','g','H','h','I','i','J','j','K','k','L','l','M','m','N','n','O','o','P','p','Q','q','R','r','S','s','T','t','U','u','V','v','W','w','X','x','Y','y','z','Z','0','1','2','3','4','5','6','7','8','9','.',',','?','\'','!','/','(',')','&','@'};
char b[]={".-",".-","-...","-...","-.-.","-.-.","-..","-..",".",".","..-.","..-.","--.","--.","....","....","..","..",".---",".---","-.-","-.-",".-..",".-..","--","--","-.","-.","---","---",".--.",".--.","--.-","--.-",".-.",".-.","...","...","-","-","..-","..-","...-","...-",".--",".--","-..-","-..-","-.--","-.--","--..","--..","-----","-----",".----",".----","..---","..---","...--","...--","....-","....-",".....",".....","-....","-....","--...","--...","---..","---..","----.","----.",".-.-.-",".-.-.-","--..--","--..--","..--..","..--..",".----.",".----.","-.-.-","-.-.--","-..-.","-..-.","-.--.","-.--.","-.--.-","-.--.-",".-...",".-..."};
char c[40];
cout<<"Enter code ";
cin.getline(c,40);
for(int i=0;i<1;i++){
for(int j=0;j<72;j++){
if(b[j]==c[i]){
cout<<a[j];
}
}
}
return 0;
}
回答by David Heffernan
You have said that b
is an array of char
. But you are supplying string literals rather than individual characters. It's impossible to know what you really mean to do. Perhaps you actually want b
to be an array of strings:
你说过这b
是一个char
. 但是您提供的是字符串文字而不是单个字符。不可能知道你真正想做什么。也许你真的想b
成为一个字符串数组:
const char* b[] = {".-", ".-", "-...", "-...", ...};
回答by bblincoe
A char
array cannot contain strings! You should initialize it with individual char
s!
一个char
数组不能包含字符串!你应该用单独的char
s初始化它!
For example: char b[] = { 'I', 'O', 'U' };
例如: char b[] = { 'I', 'O', 'U' };
When you desire string literals, you can use the following:
当您需要字符串文字时,您可以使用以下内容:
const char* b[] = { "II", "OO", "UU" };
回答by Eric Fortin
Either b
should be an array of const char*
or you should put char in the initializer list instead of string literals.
要么b
应该是一个数组,const char*
要么你应该把 char 放在初始化列表中而不是字符串文字中。
const char* b[]={".-",".-", ...}
or
或者
char b[] = {'a', 'b', ...};
回答by Mad Physicist
You are not trying to store char
s in b
, but strings. Declare b
as const char *b[72] = ...
instead.
您不是要char
在 s 中存储s b
,而是在字符串中存储。声明b
作为const char *b[72] = ...
代替。
回答by SHIWAM KUMAR PRASAD
write
写
static const char *a[72] =
{
'A', 'a', 'B', 'b', 'C', 'c', 'D', 'd', 'E', 'e', 'F', 'f', 'G', 'g',
'H', 'h', 'I', 'i', 'J', 'j', 'K', 'k', 'L', 'l', 'M', 'm', 'N', 'n',
'O', 'o', 'P', 'p', 'Q', 'q', 'R', 'r', 'S', 's', 'T', 't', 'U', 'u',
'V', 'v', 'W', 'w', 'X', 'x', 'Y', 'y', 'z', 'Z', '0', '1', '2', '3',
'4', '5', '6', '7', '8', '9', '.', ',', '?', '\'', '!', '/', '(', ')',
'&', '@'
};
static const char *b[] =
{
".-", ".-", "-...", "-...", "-.-.", "-.-.", "-..", "-..", ".", ".",
"..-.", "..-.", "--.", "--.", "....", "....", "..", "..", ".---", ".---",
"-.-", "-.-", ".-..", ".-..", "--", "--", "-.", "-.", "---", "---",
".--.", ".--.", "--.-", "--.-", ".-.", ".-.", "...", "...", "-", "-",
"..-", "..-", "...-", "...-", ".--", ".--", "-..-", "-..-", "-.--", "-.--",
"--..", "--..", "-----", "-----", ".----", ".----", "..---", "..---", "...--",
"...--", "....-", "....-", ".....", ".....", "-....", "-....",
"--...", "--...", "---..", "---..", "----.", "----.", ".-.-.-",
".-.-.-", "--..--", "--..--", "..--..", "..--..", ".----.", ".----.",
"-.-.-", "-.-.--", "-..-.", "-..-.", "-.--.", "-.--.", "-.--.-", "-.--.-",
".-...", ".-..."
};
I think it will help you...
我想它会帮助你...