I can't get those variable declarations right...Why those parameters don't work? I just set the n
value as arrays length.
Why the compiler can understand the variable declaration?
void diag(int n, double a[n][n], double b[n],double x[n]){
for(int i = 1; i <= n; i++){
x[i] = b[i]/a[i][i];
}
return x;
}
You have to use dynamic arrays (for example std::vector) or pointer on array. In C++ You can't use arrays with not known size at compilation time ('n' is not know).
This is just not legal C++ or anything close to it. Arrays must have their dimension known at compile-time, and if you pass one to a function, all dimensions except for the innermost must be specified.
Furthermore, the name of a parameter is not in scope for other parameter declarations. (which is what your errors are about).
If you want to use "arrays" whose dimension is not known at compile-time, you can use std::vector
which is a contiguous resizeable array.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.