[英]Typecasting from integer to double
Below is the basic piece of code that I am trying to run 以下是我尝试运行的基本代码
double h_a[9],h_b[2500],h_c[2704];
int r_a,c_a,r_b,c_b,r_c,c_c;
r_a = c_a = 3;
r_b = c_b = 50;
r_c = r_a + r_b - 1;
c_c = c_a + c_b - 1;
for(int i=0;i<(r_a*c_a);i++)
h_a = double(rand() % 50 + 1);
for(i=0;i<(r_b*c_b);i++)
h_b = rand() % 50 + 1;
It is showing me the following errors: 1. incompatible types in assignment of 'double' to 'double [9] 2. name lookup of 'i' changed for ISO 'for' scoping [-fpermissive]| 它向我显示以下错误:1.将'double'分配给'double [9]时不兼容的类型2.为ISO'for'作用域更改了'i'的名称查找[-fpermissive] | 3. incompatible types in assignment of 'int' to 'double [2500]'
3.在将“ int”分配给“ double [2500]”时类型不兼容
Help will be appreciated. 帮助将不胜感激。
h_a
and h_b
are arrays. h_a
和h_b
是数组。 You can't assign a value to an array, only to an element of an array. 您不能将值分配给数组,而只能分配给数组的元素。
Replacing 更换
h_a = double(rand() % 50 + 1);
by 通过
h_a[0] = double(rand() % 50 + 1);
and making a similar change in the assignment to h_b
would satisfy the compiler. 并对
h_b
的分配进行类似的更改将使编译器满意。 I have no idea whether it would be correct. 我不知道这是否正确。
You have two for
loops. 您有两个
for
循环。 The first defines the loop variable in the loop header; 第一个在循环头中定义循环变量。 the second does not:
第二个不:
for (int i = blah; blah; blah) { ... }
for (i = blah; blah; blah) { ... }
The scope of the i
defined in the first loop is just the loop. 在第一个循环中定义的
i
的范围就是循环。 It's not visible in the second loop. 在第二个循环中不可见。 But in an old version of C++, the scope extended to the block enclosing the loop.
但是在旧版本的C ++中,范围扩展到了包含循环的块。 Under those rules, it would have been legal.
根据这些规则,这将是合法的。 Apparently the compiler still recognizes the old rules.
显然,编译器仍然可以识别旧规则。 Change
i = ...
in the second loop to int i = ...
. 在第二个循环中将
i = ...
更改为int i = ...
You'll then have two distinct variables, both named i
, one for each loop. 然后,您将有两个不同的变量,都命名为
i
,每个循环一个。
You are assigning an array's name with a value, which is wrong. 您正在为数组的名称分配一个值,这是错误的。 You can assign it to an element of the array.
您可以将其分配给数组的元素。 i is declared in first for loop.
我在第一个for循环中声明。 That's why it is not visible in second for loop.
这就是为什么它在第二个for循环中不可见的原因。 declare it before first loop starts.
在第一个循环开始之前声明它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.