Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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++新手,我想了解函数中数组的行为。这是矩阵向量乘法的一种尝试 #include <stdio.h> #define N 4 float Gi[N][N] = {{ 0.0, 0.0, 0.0, 1.0}, { 0.0, 2.57142857, 3.42857143, 1.28571429}, {-0.0, 3.42857143, 8.57142857, 1.71428571}, { 1.0, 1.28571429, 1.71428571, 0.14285714}}; void mult( float vec_I[N], float vec_V[N]){ int acc; for(int i = 0; i<N; i++){ acc = 0; for(int j = 0; j<N; j++){ acc += Gi[i][j]*vec_I[j]; } vec_V[i] = acc; } } float solver(){ float out[N]; float I[N] = {0.0, 0.0, 0.0, 10.0}; float V[N] = {0.0, 0.0, 0.0, 0.0}; mult(I, V); out[0] = V[0]; out[1] = V[1]; out[2] = V[2]; out[3] = V[3]; return out; } int main(){ float outPrint[4]; outPrint = solver(); printf("| %d |\n", outPrint[0]); printf("| %d |\n", outPrint[1]); printf("| %d |\n", outPrint[2]); printf("| %d |\n", outPrint[3]); return 0; } #包括 #定义n4 float Gi[N][N]={{0.0,0.0,0.0,1.0}, { 0.0, 2.57142857, 3.42857143, 1.28571429}, {-0.0, 3.42857143, 8.57142857, 1.71428571}, { 1.0, 1.28571429, 1.71428571, 0.14285714}}; 空骡( 浮动向量I[N], 浮动向量(V[N]){ 国际会计准则; for(int i=0;i_C++_Arrays_Function - Fatal编程技术网

错误无法转换';浮动*';至';浮动';作为回报 我是C++新手,我想了解函数中数组的行为。这是矩阵向量乘法的一种尝试 #include <stdio.h> #define N 4 float Gi[N][N] = {{ 0.0, 0.0, 0.0, 1.0}, { 0.0, 2.57142857, 3.42857143, 1.28571429}, {-0.0, 3.42857143, 8.57142857, 1.71428571}, { 1.0, 1.28571429, 1.71428571, 0.14285714}}; void mult( float vec_I[N], float vec_V[N]){ int acc; for(int i = 0; i<N; i++){ acc = 0; for(int j = 0; j<N; j++){ acc += Gi[i][j]*vec_I[j]; } vec_V[i] = acc; } } float solver(){ float out[N]; float I[N] = {0.0, 0.0, 0.0, 10.0}; float V[N] = {0.0, 0.0, 0.0, 0.0}; mult(I, V); out[0] = V[0]; out[1] = V[1]; out[2] = V[2]; out[3] = V[3]; return out; } int main(){ float outPrint[4]; outPrint = solver(); printf("| %d |\n", outPrint[0]); printf("| %d |\n", outPrint[1]); printf("| %d |\n", outPrint[2]); printf("| %d |\n", outPrint[3]); return 0; } #包括 #定义n4 float Gi[N][N]={{0.0,0.0,0.0,1.0}, { 0.0, 2.57142857, 3.42857143, 1.28571429}, {-0.0, 3.42857143, 8.57142857, 1.71428571}, { 1.0, 1.28571429, 1.71428571, 0.14285714}}; 空骡( 浮动向量I[N], 浮动向量(V[N]){ 国际会计准则; for(int i=0;i

错误无法转换';浮动*';至';浮动';作为回报 我是C++新手,我想了解函数中数组的行为。这是矩阵向量乘法的一种尝试 #include <stdio.h> #define N 4 float Gi[N][N] = {{ 0.0, 0.0, 0.0, 1.0}, { 0.0, 2.57142857, 3.42857143, 1.28571429}, {-0.0, 3.42857143, 8.57142857, 1.71428571}, { 1.0, 1.28571429, 1.71428571, 0.14285714}}; void mult( float vec_I[N], float vec_V[N]){ int acc; for(int i = 0; i<N; i++){ acc = 0; for(int j = 0; j<N; j++){ acc += Gi[i][j]*vec_I[j]; } vec_V[i] = acc; } } float solver(){ float out[N]; float I[N] = {0.0, 0.0, 0.0, 10.0}; float V[N] = {0.0, 0.0, 0.0, 0.0}; mult(I, V); out[0] = V[0]; out[1] = V[1]; out[2] = V[2]; out[3] = V[3]; return out; } int main(){ float outPrint[4]; outPrint = solver(); printf("| %d |\n", outPrint[0]); printf("| %d |\n", outPrint[1]); printf("| %d |\n", outPrint[2]); printf("| %d |\n", outPrint[3]); return 0; } #包括 #定义n4 float Gi[N][N]={{0.0,0.0,0.0,1.0}, { 0.0, 2.57142857, 3.42857143, 1.28571429}, {-0.0, 3.42857143, 8.57142857, 1.71428571}, { 1.0, 1.28571429, 1.71428571, 0.14285714}}; 空骡( 浮动向量I[N], 浮动向量(V[N]){ 国际会计准则; for(int i=0;i,c++,arrays,function,C++,Arrays,Function,数组不是一等公民。因此,它们不能按值返回。因此,必须使用结构包装器: struct matrix_t { float data[16]; }; 您希望solver返回什么?是一个数字,还是一个数字数组?看起来您是想让它返回一个数组使用std::array替换所有数组将非常容易解决此问题,但也要问自己,“是否要返回一个即将超出范围并变得无效的局部变量?”正确使用new和malloc会带来大量的心理负担,而且很多时候还需要大量的调试,所以更愿意使用上面Mooing Duck提到的std::ar

数组不是一等公民。因此,它们不能按值返回。因此,必须使用结构包装器:

struct matrix_t
{
  float data[16];
};

您希望
solver
返回什么?是一个数字,还是一个数字数组?看起来您是想让它返回一个数组使用
std::array
替换所有数组将非常容易解决此问题,但也要问自己,“是否要返回一个即将超出范围并变得无效的局部变量?”正确使用
new
malloc
会带来大量的心理负担,而且很多时候还需要大量的调试,所以更愿意使用上面Mooing Duck提到的
std::array
。专家们不会接触
new
malloc
,或任何其他手动管理的动态分配il他们必须这样做,你也不应该这样做。或者只是通过引用将数组传递到函数中并以这种方式修改它。让外界担心范围:)结构解决方案工作得很好。非常感谢!