【问题标题】:error: return value type does not match the function type错误:返回值类型与函数类型不匹配
【发布时间】:2013-09-30 02:38:44
【问题描述】:

这是一个代码:

#include<stdio.h>
#include<math.h>

float conv2D(int rowsKernel, int colsKernel, int rowsImage, int colsImage, float kernel[][5], int image[][15], float imageConvolved[][11]); //Function Prototype definition

float conv2D(int rowsKernel, int colsKernel, int rowsImage, int colsImage, float kernel[][5], int image[][15], float imageConvolved[][11])
{
    int kernelSize;     //This variable represents the size of the Gaussian kernel
    int i;          //variable which controls the rows of an image
    int j;          //variable which controls the columns of an image
    int ii;         //variable which controls the rows of the kernel
    int jj;         //variable which controls the columns of the kernel
    float sum;          //variable that holds the result of convolution for a particular pixel of an image
//float imageConvolved;//Result of an image convolved by a Gaussian kernel
    int imagePixel;
    float kernelPixel;
    kernelSize = colsKernel;    /*Since we consider a square kernel, then rowsKernel=colsKernel, which implies that the size of the                 kernel (kernelSize) equals either of these two variables (that is, kernelSize=colsKernel=rowsKernel) */
    for (i = kernelSize / 2; i < rowsImage - kernelSize / 2; i++)   // perform iteration through the rows of an image
    {
    for (j = kernelSize / 2; j < colsImage - kernelSize / 2; j++)   // perform iteration through the columns of an image
    {
        sum = 0;        /*Initializing the accumulator. This variable will finally contain the convolution result for a particular pixel */
        for (ii = -kernelSize / 2; ii <= kernelSize / 2; ii++)  // perform iteration through the rows of a kernel
        {
        for (jj = -kernelSize / 2; jj <= kernelSize / 2; jj++)  //perform iteration through the columns of a kernel
        {
            imagePixel = image[i + ii][j + jj];
            kernelPixel = kernel[ii + kernelSize / 2][jj + kernelSize / 2];

            sum += imagePixel * kernelPixel;
        }
        }
        imageConvolved[i - kernelSize / 2][j - kernelSize / 2] = sum;
    }
    }
    return (imageConvolved);    // convolved image
}

int main()
{
    float gauss[][5] = {
    {1, 1, 1, 1, 1},
    {1, 1, 1, 1, 1},
    {1, 1, 1, 1, 1},
    {1, 1, 1, 1, 1},
    {1, 1, 1, 1, 1}
    };
    int img[][15] = {
    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
    };
    int rowsK = 5, colsK = 5, rowsI = 15, colsI = 15;
    float result[11][11];
    float Iconv[11][11];
    Iconv = conv2D(rowsK, colsK, rowsI, colsI, gauss, img, result);
    return 0;
}

我修改了代码以尝试解决问题。但是,我遇到了两个错误(使用 CCStudio V3.3):

“convolution.c”,第 39 行:错误:返回值类型与 函数类型“convolution.c”,第 71 行:错误:表达式必须是 可修改的左值

【问题讨论】:

  • 请粘贴真实代码,包括每个参数的声明及其类型,as code;不是埋在一个段落里。您的描述“此函数中的参数声明为浮点结果、int rowsK、int colsK、int rowsI 和 int colsI”很方便地缺少最后两种参数类型,如果不明显,它们是至关重要的找到你的问题。因此具有讽刺意味的是,它们不包括在内。简而言之,不要只告诉我们“关于”您的代码; 显示它
  • 什么是imageConvolved?您将其作为 2D 数组访问,并将其作为 float 返回。你要哪个?另外,我同意上面的评论。你的问题很难看。将代码包含为代码块,而不是段落中的粗体块。
  • 您说 imageConvolved 被定义为浮点数,但您将其视为 2D 数组。你从返回浮点数的函数中返回了它,所以它似乎是一个裸浮点值,你尝试将它用作二维数组。如果你只是复制并粘贴你的代码可能会更好..
  • 你是说imageConvolved 是这样声明的:float imageConvolved?在这种情况下,很清楚为什么编译器不喜欢它,因为您像访问二维数组一样访问它。
  • @ChrisWue 似乎 imageConvolved 是一个输入参数,是二维浮点数组。

标签: c arrays pointers gcc


【解决方案1】:

您正在从根据其声明返回float 的函数返回似乎是二维数组(imageConvolved)。我不知道你从函数返回的意思,但似乎错误就在这一行:

return(imageConvolved);

【讨论】:

    【解决方案2】:

    函数conv2D的原型是float。但是,在您返回的定义中 imageConvolvedimageConvolved 是你的ormal parameter 中的一个数组,你不需要返回它,所以你会返回 float insead 类型的东西。

    【讨论】:

    • 问题已经解决(当我使用GCC编译器编译时)。已经使函数 conv2D 在调用点不返回任何东西,并且在 main() 函数中,我将另一个收集结果的参数传递给函数 conv2D。然后我可以轻松地从 main() 函数读取结果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-08
    • 1970-01-01
    • 1970-01-01
    • 2014-11-10
    • 1970-01-01
    相关资源
    最近更新 更多