gpt4 book ai didi

c++ - 错误 : cannot convert 'float (*)[(((sizetype)(((ssizetype)n) + -1)) + 1)]' to 'float (*)[100]' for argument '3'

转载 作者:行者123 更新时间:2023-11-28 02:55:26 24 4
gpt4 key购买 nike

我正在尝试执行一个 bool 函数来验证矩阵是否对称,但出现此错误:

|54|error: cannot convert 'float ()[(((sizetype)(((ssizetype)n) + -1)) + 1)]' to 'float ()[100]' for argument '3' to 'void Transpose(int, float ()[100], float ()[100])'|

#include <iostream>
using namespace std;

void Transpose(int n, float a[][MAX], float T[][MAX]) {
int i,j;

for(i = 0; i < n; i++){
for(j = 0; j < n; j++){
T[i][j] = a[j][i];
}
}
}

bool Symmetric(int n, float a[][MAX]) {
float t[n][n];
int i,j;

for(i = 0; i < n; i++){
for(j = 0; j < n; j++){
t[i][j] = 0;
}
}

Transpose(n,a,t); // <--- Error here.

for(i = 0; i < n; i++){
for(j = 0; j < n; j++){
if(t[i][j] != a[i][j]){
return false;
}
}
}

return true;
}

错误发生在 Transpose(n,a,t);

最佳答案

编译器说的是 float t[n][n] 的数组,其中 n 是一个编译时变量 [也就是说,不是常量]不匹配 float T[][MAX],当 MAX 是编译时常量。

float t[n][MAX] 用于您的临时矩阵可能会很好。但是,请记住 C 和 C++ 不能很好地处理“可变大小数组”(特别是将它们从一个函数传递到另一个函数时),在 C++ 中,使用不同的方法可能是更好的主意描述你的矩阵。例如:

std::vector<vector<float>> t; 

然后您需要做一些更多的工作来定义 vector 的大小,例如:

t.resize(n);
for(i = 0; i < n; i++){
t[i].resize(n);

关于c++ - 错误 : cannot convert 'float (*)[(((sizetype)(((ssizetype)n) + -1)) + 1)]' to 'float (*)[100]' for argument '3' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22121974/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com