【发布时间】:2017-04-28 23:50:51
【问题描述】:
我正在尝试跑步,但我不能。也许它与指针有关, 我已经失去了期望。
#include <iostream>
using namespace std;
float createMatrix( int N )
{
int i,j;
float **matrix;
matrix = new float*[N];
if (matrix == NULL)
{
free(matrix);
return 0;
}
for (i=0; i<N; i++)
matrix[i] = new float[N];
for(i=0; i<N;i++)
for(j=0; j<N;j++)
matrix[i][j] = rand();
return **matrix;
}
void printMatrix (float **matrix)
{
int N = sizeof(matrix)/sizeof(float);
for(int i=0; i<N;i++)
{
for(int j=0; j<N;j++)
std::cout << (matrix)[i][j];
std:: cout << std::endl;
}
}
int main(int argc, const char * argv[]) {
int N=0;
std::cout << "Hello! Please, write any number for a size of the matrix\n";
std::cin >> N;
float m = createMatrix(N);
printMatrix(m); // error
return 0;
}
错误:没有匹配的函数来编译“printMatrix” 请帮我!
【问题讨论】:
-
printMatrix期望float**但你正在传递float -
除了
float createMatrix( int N )泄漏内存(和创建单个float而不是float矩阵)。 -
为什么
printMatrix需要一个指向指针的指针?你想做什么?你传递了一个float并期待一个**float!! -
new float*[N]从不返回 null,如果返回,free(nullptr)无效。 -
@aschepler
free(nullptr)无效 不。正如here 解释的那样:如果 ptr 是空指针,则该函数什么也不做。
标签: c++ compiler-errors compilation