没有用于调用的匹配函数
no matching function for call to
#include <iostream>
using namespace std;
int n;
void displaysum(double mat[n][n])
{
double sum= 0;
for(int j=0;j<n;j++)
sum += mat[j][j];
cout<<"Sum of Diagnols Elements is n"<<sum;
}
int main()
{
cout << "what are the number of rows or column in the matrix" << endl;
cin >> n;
double matrix[n][n];
for (int row = 0; row < n; row++)
{
for (int column = 0; column < n; column++)
cin >> matrix[row][column];
}
displaysum(matrix)
return 0;
}
我不明白为什么我收到一个错误,因为没有匹配的函数可以调用XCODE
.即使我尝试更改函数原型中的变量,它仍然会给我同样的错误。
我不明白为什么我收到一个错误,因为没有匹配的函数可以调用
XCODE
.
基本问题是C++希望第二维在编译时是常量,以便进行类型检查。如果你想解决这个问题,你必须使用指针(AFAIK)。您可以通过将displaysum
声明更改为
void displaysum(double **mat)
并为原始函数中的matrix
进行适当的分配。
如果你不喜欢这个,那么,欢迎来到C++的类型系统。在函数声明中,double mat[n][n]
被视为double (*)[n]
。这实际上是有道理的,但为什么它不认为matrix
属于那种类型,是因为n
不是恒定的。您可以更改呼叫
displaysum(matrix);
对此:
displaysum(static_cast<double (*)[n]>(matrix);
并收到奇怪的错误
static_cast from 'double (*)[n]' to 'double (*)[n]' is not allowed
(这不是你从类型系统中得到的最奇怪的错误)
相关文章:
- 函数调用中参数的顺序重要吗
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 变量没有改变?通过向量的函数调用
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 是否有C++编译器选项允许激进地删除所有函数调用,并将参数传递给具有空体的函数
- 我知道函数调用中存在歧义.有没有办法调用foo()函数
- 模板函数调用
- 获取从C++中同一类中的构造函数调用的方法返回的值
- 析构函数调用
- 成员函数调用和C++对象模型
- 使用共享指针的函数调用,其对象应为 const
- C++:编译时检查匹配的函数调用对?
- 函数调用C++中的参数太少
- 来自 DLL 的函数调用 [表观调用的括号前面的表达式必须具有(指向-)函数类型]
- 返回指向对象的指针的函数调用是否为 prvalue?
- C++ 如何重载 [] 运算符并进行函数调用
- 代码的效率. 转到和函数调用
- 是同一作用域的函数部分中的函数调用
- 如何封装一个函数,以便它只能由同一类中的一个其他函数调用?
- 类型擦除的std::function与虚拟函数调用的开销