我不断收到错误C2440'=':无法从'int *(__ cdecl *)(int *,int *,int,int)'转换为'int *

时间:2019-12-14 17:01:17

标签: c++

c = sum_matrix部分,我一直遇到错误。

  

C2440'=':无法从'int *(__ cdecl *)(int *,int *,int,int)'转换为'int *

这是整个代码(虽然我被卡在这里还没有完成):

#define _CRT_SECURE_NO_WARNINGS
#include <conio.h>
#include <cstdio>

void read_matrix(int*, int, int);
int* sum_matrix(int*, int*, int, int);

void main()
{
    int a[20][20], b[20][20], i, n = 0, m = 0, * p, * q, * c;
    printf_s("Enter the dimensions of the matrices: ");
    scanf_s("%d %d", n, m);
    p = &a[0][0];
    q = &b[0][0];
    read_matrix(p, n, m);
    read_matrix(q, n, m);
    c = sum_matrix;
}

int* sum_matrix(int* a, int* b, int n, int m)
{
    int *c, i, j, p;
    if (c = new int[n*m])
        for (i = 0; i < n; i++)
            for (j = 0; j < m; j++)
                *(c + i * n + j) = *(a + i * n + j) + *(b + i * n + j);
    return c;
}

void read_matrix(int* a, int n, int m)
{
    int i = 0, j = 0;
    printf_s("Enter the elements of the matrix (%d values): ", n * m);
    for (i = 0; i < n; i++)
        for (j = 0; j < m; j++)
            scanf_s("%d", (a + i * n + j));
}

我正在使用Visual Studio 16.3.8

3 个答案:

答案 0 :(得分:5)

sum_matrix是一个函数,您需要像这样用括号括起来:

sum_matrix(a, b, c, d)

用要输入的参数替换a,b,c,d。

我认为您的意思是这样

#define _CRT_SECURE_NO_WARNINGS
#include <conio.h>
#include <cstdio>

void read_matrix(int*, int, int);
int* sum_matrix(int*, int*, int, int);

void main()
{
    int a[20][20], b[20][20], i, n = 0, m = 0, * p, * q, * c;
    printf_s("Enter the dimensions of the matrices: ");
    scanf_s("%d %d", n, m);
    p = &a[0][0];
    q = &b[0][0];
    read_matrix(p, n, m);
    read_matrix(q, n, m);
    c = sum_matrix(a, b, n, m);
}

int* sum_matrix(int* a, int* b, int n, int m)
{
    int *c, i, j, p;
    if (c = new int[n*m])
        for (i = 0; i < n; i++)
            for (j = 0; j < m; j++)
                *(c + i * n + j) = *(a + i * n + j) + *(b + i * n + j);
    return c;
}

void read_matrix(int* a, int n, int m)
{
    int i = 0, j = 0;
    printf_s("Enter the elements of the matrix (%d values): ", n * m);
    for (i = 0; i < n; i++)
        for (j = 0; j < m; j++)
            scanf_s("%d", (a + i * n + j));
}

答案 1 :(得分:2)

通常,您要分配c = sum_matrix,其中cint*,而sum_matrix是具有签名int* sum_matrix(int*, int*, int, int)的函数

答案 2 :(得分:0)

python setup.py install中,main没有输入参数。它应该是sum_matrix。另外,c = sum_matrix(a, b, n, m)应该仅声明为c,而不是指向intint)的指针。