Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 编译错误:分配给';int';来自不兼容类型';char';尝试将数组分配给数组时_C++ - Fatal编程技术网

C++ 编译错误:分配给';int';来自不兼容类型';char';尝试将数组分配给数组时

C++ 编译错误:分配给';int';来自不兼容类型';char';尝试将数组分配给数组时,c++,C++,我正试图写一个程序,它会有一个列表,就像这样 {{},{},{},{} 其中每个列表将是一个y值,其中包含的是x值 当我试图编译我的程序时,标题中出现了错误 该计划的相关部分: void genRand(){ int x; int y; cin >> x; cin >> y; int world[y]; for(int yv = 0; yv == y; yv++){ char ylist[x]; for(int xv = 0; xv == x

我正试图写一个程序,它会有一个列表,就像这样

{{},{},{},{}

其中每个列表将是一个y值,其中包含的是x值

当我试图编译我的程序时,标题中出现了错误

该计划的相关部分:

void genRand(){

int x;
int y;

cin >> x;
cin >> y;

int world[y];


for(int yv = 0; yv == y; yv++){

    char ylist[x];

    for(int xv = 0; xv == x; xv++){

        char symbols[2] = {'-', '#'};

        int randIndex = rand() % 2;

        ylist[xv] = symbols[randIndex];
    }

    world[yv] = ylist;


}


error: assigning to 'int' from
      incompatible type 'int [x]'
                world[yv] = ylist;
                            ^

我怎样才能解决这个问题?它在上面的行中工作正常,但再次尝试时会突然中断。

您正在尝试将
字符串
值放入
int
数组中


int-world[y]
应该是
string
array,所以将其更改为
string-world[y]
ylist
->字符数组类型

char ylist[x];
world[yv]
->integer

int world[y];
导致错误的问题是,我们正在将字符数组类型设置为整数类型


我们可以通过实际更改数据类型来解决此问题,方法是设置的数据类型与处理程序相同。

报告的转换问题:

错误:从分配到“int”

不兼容类型“int[x]”

世界[yv]=ylist

所以请记住,数组只是存储在连续内存块中的多个数据值。“char”和“int”在内存中的格式完全不同,因此如果您尝试将它们作为不同的数据类型读取,您将得到意外的结果。实际上,“char”在内存中仅为1字节,“int”为4字节,因此,如果要将ASCII码存储为整数,则需要在将其存储为int之前对其进行转换

我相信这可以解决您的问题:

    int num = static_cast<int>(letter); // if letter='A', num=10
int num=static_cast(字母);//如果字母class='A',num=10

[注意]-ASCII数字值从0到9开始,然后是字母字符。考虑使用异常处理来捕获不良数据是个好主意。

1)VLAS是非标准C++。2) 您正试图将
char
s数组分配给单个
int
。这项任务的意义是什么?考虑从A学习,而不是随机编码。3) 这种错误在显示的代码中是不可能的,因为显示的错误抱怨
int[]
int
的转换,而代码试图将
char[]
分配到
int
。我正试图通过转到world[yv]来索引世界列表。所以它每次通过循环时都会起作用,[have list,用字符填充列表,将该列表放在更大的列表中,have list,等等]您仍然没有解释,当您将一个
char
s数组分配给单个
int
时,会发生什么。考虑学习,正如我已经提到过的那样,for循环的中间部分是一个while条件,而不是一个条件。因此,使用
会更好=
而不是
==
。如果
ylist
是一个C字符串,谁在其中放入终止的
\0
?您的更改会导致未定义的行为。