c - 对特定功能使用未声明的标识符
问题描述
我弹出以下错误代码:
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
以外的所有其他函数)没有这个问题。我检查了拼写错误并在谷歌上查找是什么意思。但是没有帮助,因为错误只发生在我犯的错误上。swap
value
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;
}
解决方案
您没有height
或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){ ^
实际上,函数定义的范围height
和范围是什么?范围内没有声明,既不是来自文件范围,也不是来自函数定义中的其他地方。与之相比,这显然被编译器接受:width
value
void sepia(int height, int width, RGBTRIPLE image[height][width]){
请注意sepia()
hasheight
和width
参数。那个函数的外观中的height
and指的是这些。width
image[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 本身更早地出现在参数列表中。
推荐阅读
- javascript - Bootstrap Modal Couldn't Refresh the Data
- node.js - Why getting error when request docusign JWT token
- ios - How to reload Linechart Data which is coming from external API in Swift?
- java - How to fix java.lang.SecurityException Android 4.2.2
- excel - 使用do while我如何填充等于数据透视表中特定单元格值的空白?
- hyperledger-fabric - 如何将 RCA PEM 文件从 RCA 复制到 ICA?
- c# - Line Renderer 的线在 (0, 0, 0) 处保持连接
- sql-server - 如何修复“启动用户态代理时出错:在 Windows 10 上使用 docker 侦听 tcp 0.0.0.0:1433
- sql-server - 如何将此存储过程从 SQL Server 转换为 Postgresql?
- sql-server - Disable_error_and_usage_SQLserver