使用未声明的标识符发生在特定功能上

Use of undeclared identifier happening to specific function

我弹出以下错误代码:

In file included from filter.c:5:
./helpers.h:16:27: error: use of undeclared identifier 'height'
void swap(RGBTRIPLE image[height][width], int row, int pix, int width);
                          ^
./helpers.h:16:35: error: use of undeclared identifier 'width'
void swap(RGBTRIPLE image[height][width], int row, int pix, int width);
                                  ^
./helpers.h:21:33: error: use of undeclared identifier 'height'
RGBTRIPLE value(RGBTRIPLE image[height][width], int x, int y);
                                ^
./helpers.h:21:41: error: use of undeclared identifier 'width'
RGBTRIPLE value(RGBTRIPLE image[height][width], int x, int y);
                                        ^
4 errors generated.
In file included from helpers.c:1:
./helpers.h:16:27: error: use of undeclared identifier 'height'
void swap(RGBTRIPLE image[height][width], int row, int pix, int width);
                          ^
./helpers.h:16:35: error: use of undeclared identifier 'width'
void swap(RGBTRIPLE image[height][width], int row, int pix, int width);
                                  ^
./helpers.h:21:33: error: use of undeclared identifier 'height'
RGBTRIPLE value(RGBTRIPLE image[height][width], int x, int y);
                                ^
./helpers.h:21:41: error: use of undeclared identifier 'width'
RGBTRIPLE value(RGBTRIPLE image[height][width], int x, int y);
                                        ^
helpers.c:59:27: error: use of undeclared identifier 'height'
void swap(RGBTRIPLE image[height][width], int row, int pix, int width){
                          ^
helpers.c:59:35: error: use of undeclared identifier 'width'
void swap(RGBTRIPLE image[height][width], int row, int pix, int width){
                                  ^
helpers.c:78:33: error: use of undeclared identifier 'height'
RGBTRIPLE value(RGBTRIPLE image[height][width], int x, int y){
                                ^
helpers.c:78:41: error: use of undeclared identifier 'width'
RGBTRIPLE value(RGBTRIPLE image[height][width], int x, int y){
                                        ^
8 errors generated.
Makefile:2: recipe for target 'filter' failed
make: *** [filter] Error 1

我真的不明白为什么我做的函数有undeclared indentifier错误,而预制函数(除swapvalue之外的所有其他函数)却没有有这个问题。我检查了拼写错误并查找了 google use of undeclared identifier 的意思。但是没有帮助,因为错误只发生在我做的那些上。

#include "helpers.h"
#include <math.h>
#include <cs50.h>

// Convert image to grayscale
void grayscale(int height, int width, RGBTRIPLE image[height][width]){
    int average = 0;
    RGBTRIPLE dot;

    for(int i = 0; i < height; i++){
        for(int j = 0; j < width; j++){
            dot = image[i][j];
            average = round((dot.rgbtRed + dot.rgbtGreen + dot.rgbtBlue) / 3);
        }
    }
    return;
}

int max(int value){
    if(value > 255){
        return 255;
    }
    else{
        return value;
    }
}

// Convert image to sepia
void sepia(int height, int width, RGBTRIPLE image[height][width]){
    RGBTRIPLE dot;

    for(int i = 0; i < height; i++){
        for(int j = 0; j < width; j++){
            dot = image[i][j];
            image[i][j].rgbtRed = max(round(.393 * dot.rgbtRed + .769 * dot.rgbtGreen + .189 * dot.rgbtBlue));
            image[i][j].rgbtGreen = max(round(.349 * dot.rgbtRed + .686 * dot.rgbtGreen + .168 * dot.rgbtBlue));
            image[i][j].rgbtBlue = max(round(.272 * dot.rgbtRed + .534 * dot.rgbtGreen + .131 * dot.rgbtBlue));
        }
    }
    return;
}

// Reflect image horizontally
void reflect(int height, int width, RGBTRIPLE image[height][width]){
    int n;

    if(width % 2 != 0){
        n = 1;
    }

    for(int i = 0; i < height; i++){
        for(int j = 0, k = (width - n) / 2; j < k; j++){
            swap(image, i, j, width);
        }
    }
    return;
}

void swap(RGBTRIPLE image[height][width], int row, int pix, int width){
    RGBTRIPLE temp;

    temp = image[row][pix];
    image[row][pix] = image[row][width - pix];
    image[row][width - pix] = temp;
}

// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width]){

    for(int i = 0; i < height; i++){
        for(int j = 0; j < width; j++){
            image[i][j] = value(image, i, j)
        }
    }
    return;
}

RGBTRIPLE value(RGBTRIPLE image[height][width], int x, int y){
    int count = 0;
    int red;
    int blue;
    int green;
    RGBTRIPLE dot;


    for(int i = -1; i <= 1; i++){
        for(int j = -1; j <= 1; j++){
            int height = i + x;
            int width = j + y;

            if((height >= 0) && (width >= 0)){
                red += image[height][width].rgbtRed;
                blue += image[height][width].rgbtBlue;
                green += image[height][width].rgbtGreen;
                count++;
            }
        }
    }

    dot.rgbtRed = round(red / count);
    dot.rgbtBlue = round(blue / count);
    dot.rgbtGreen = round(green / count);

    return dot;
}

这是因为 heightwidth 在这些函数中没有在 RGBTRIPLE image[height][width] 之前声明,而它们在不发出错误的函数中声明。

您应该在 image 之前添加 heightwidth 就像其他函数一样:

void swap(int height, int width, RGBTRIPLE image[height][width], int row, int pix){
RGBTRIPLE value(int height, int width, RGBTRIPLE image[height][width], int x, int y){

您没有在文件范围内声明 heightwidth 个对象。

函数原型中不属于函数定义的参数声明的范围在原型末尾结束。这意味着,除其他外,

  • 同一函数的多个原型在参数名称方面不需要保持一致,并且
  • in-scope 函数原型不为同一函数的其他原型提供参数声明,无论是否是函数定义的一部分。

然后考虑这些相关的诊断:

helpers.c:78:33: error: use of undeclared identifier 'height'
RGBTRIPLE value(RGBTRIPLE image[height][width], int x, int y){
                                ^
helpers.c:78:41: error: use of undeclared identifier 'width'
RGBTRIPLE value(RGBTRIPLE image[height][width], int x, int y){
                                        ^

在函数value的定义范围内,heightwidth确实是什么?范围内没有声明,无论是来自文件范围还是来自函数定义的其他地方。与此相比,这显然被编译器接受:

void sepia(int height, int width, RGBTRIPLE image[height][width]){

注意 sepia()heightwidth 参数。 image[height][width]那个函数里的heightwidth指的就是这些。

此外,函数原型中参数声明的范围在声明之后立即开始。这意味着参数声明不在参数列表前面的范围内。该效果在此诊断中可见:

./helpers.h:16:35: error: use of undeclared identifier 'width'
void swap(RGBTRIPLE image[height][width], int row, int pix, int width);
                                  ^

请注意,虽然该函数有一个 width 参数,但它出现在 之后 image 在参数列表中,因此它不在范围内image.

的声明

如果要使用 variable-length 数组参数,请遵循编译器接受的函数模型。确定 VLA 尺寸的参数必须比 VLA 本身更早出现在参数列表中。