首页 > 解决方案 > 对特定功能使用未声明的标识符

问题描述

我弹出以下错误代码:

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

我真的不明白为什么我制作的函数有错误,而预制函数(除andundeclared indentifier以外的所有其他函数)没有这个问题。我检查了拼写错误并在谷歌上查找是什么意思。但是没有帮助,因为错误只发生在我犯的错误上。swapvalueuse 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;
}

标签: cfunctioncs50

解决方案


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

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

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

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

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){
                                        ^

实际上,函数定义的范围height和范围是什么?范围内没有声明,既不是来自文件范围,也不是来自函数定义中的其他地方。与之相比,这显然被编译器接受:widthvalue

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

请注意sepia()hasheightwidth参数。那个函数的外观中的heightand指的是这些。widthimage[height][width]

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

./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.

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


推荐阅读