已声明原型的函数的隐式声明
Implicit declaration of function of a declared prototype
我有这个错误代码:
helpers.c:56:13: warning: implicit declaration of function 'swap' is invalid in
C99 [-Wimplicit-function-declaration]
swap(height, width, image, i, j);
^
helpers.c:62:6: error: conflicting types for 'swap'
void swap(int height, int width, RGBTRIPLE image[height][width], int row...
^
helpers.c:56:13: note: previous implicit declaration is here
swap(height, width, image, i, j);
^
helpers.c:75:31: warning: implicit declaration of function 'value' is invalid in
C99 [-Wimplicit-function-declaration]
image_new[i][j] = value(height, width, image, i, j);
^
helpers.c:75:29: error: assigning to 'RGBTRIPLE' from incompatible type 'int'
image_new[i][j] = value(height, width, image, i, j);
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
helpers.c:87:11: error: conflicting types for 'value'
RGBTRIPLE value(int height, int width, RGBTRIPLE image[height][width], i...
^
helpers.c:75:31: note: previous implicit declaration is here
image_new[i][j] = value(height, width, image, i, j);
^
我知道 implicit declaration of function
与没有函数原型有关,但在我的情况下我有一个。
helpers.c
#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.0);
image[i][j].rgbtRed = average;
image[i][j].rgbtGreen = average;
image[i][j].rgbtBlue = average;
}
}
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]){
for(int i = 0; i < height; i++){
for(int j = 0; j < width; j++){
RGBTRIPLE dot = image[i][j];
image[i][j].rgbtRed = max(round(0.393 * dot.rgbtRed + 0.769 * dot.rgbtGreen + 0.189 * dot.rgbtBlue));
image[i][j].rgbtGreen = max(round(0.349 * dot.rgbtRed + 0.686 * dot.rgbtGreen + 0.168 * dot.rgbtBlue));
image[i][j].rgbtBlue = max(round(0.272 * dot.rgbtRed + 0.534 * dot.rgbtGreen + 0.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(height, width, image, i, j);
}
}
return;
}
void swap(int height, int width, RGBTRIPLE image[height][width], int row, int pix){
RGBTRIPLE 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]){
RGBTRIPLE image_new[height][width];
for(int i = 0; i < height; i++){
for(int j = 0; j < width; j++){
image_new[i][j] = value(height, width, image, i, j);
}
}
for(int i = 0; i < height; i++){
for(int j = 0; j < width; j++){
image[i][j] = image_new[i][j];
}
}
return;
}
RGBTRIPLE value(int height, int width, RGBTRIPLE image[height][width], int x, int y){
int count = 0;
int red = 0;
int blue = 0;
int green = 0;
RGBTRIPLE dot;
for(int i = -1; i <= 1; i++){
for(int j = -1; j <= 1; j++){
int length = i + x;
int spread = j + y;
if((length >= 0) && (length < height) && (spread >= 0) && (spread < width)){
red += image[length][spread].rgbtRed;
blue += image[length][spread].rgbtBlue;
green += image[length][spread].rgbtGreen;
count++;
}
}
}
dot.rgbtRed = round((float)red / count);
dot.rgbtBlue = round((float)blue / count);
dot.rgbtGreen = round((float)green / count);
return dot;
}
helpers.h
#include "bmp.h"
// Convert image to grayscale
void grayscale(int height, int width, RGBTRIPLE image[height][width]);
// Check if color value is within limit
int max(int value);
// Convert image to sepia
void sepia(int height, int width, RGBTRIPLE image[height][width]);
// Reflect image horizontally
void reflect(int height, int width, RGBTRIPLE image[height][width]);
// Swapping pixel
void swap(int height, int width, RGBTRIPLE image[height][width], int row, int pix);
// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width]);
RGBTRIPLE value(int height, int width, RGBTRIPLE image[height][width], int x, int y);
bmp.h
// BMP-related data types based on Microsoft's own
#include <stdint.h>
/**
* Common Data Types
*
* The data types in this section are essentially aliases for C/C++
* primitive data types.
*
* Adapted from http://msdn.microsoft.com/en-us/library/cc230309.aspx.
* See http://en.wikipedia.org/wiki/Stdint.h for more on stdint.h.
*/
typedef uint8_t BYTE;
typedef uint32_t DWORD;
typedef int32_t LONG;
typedef uint16_t WORD;
/**
* BITMAPFILEHEADER
*
* The BITMAPFILEHEADER structure contains information about the type, size,
* and layout of a file that contains a DIB [device-independent bitmap].
*
* Adapted from http://msdn.microsoft.com/en-us/library/dd183374(VS.85).aspx.
*/
typedef struct
{
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
} __attribute__((__packed__))
BITMAPFILEHEADER;
/**
* BITMAPINFOHEADER
*
* The BITMAPINFOHEADER structure contains information about the
* dimensions and color format of a DIB [device-independent bitmap].
*
* Adapted from http://msdn.microsoft.com/en-us/library/dd183376(VS.85).aspx.
*/
typedef struct
{
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
} __attribute__((__packed__))
BITMAPINFOHEADER;
/**
* RGBTRIPLE
*
* This structure describes a color consisting of relative intensities of
* red, green, and blue.
*
* Adapted from http://msdn.microsoft.com/en-us/library/aa922590.aspx.
*/
typedef struct
{
BYTE rgbtBlue;
BYTE rgbtGreen;
BYTE rgbtRed;
} __attribute__((__packed__))
RGBTRIPLE;
我想不出为什么这是错误的。
当我这样做时,代码编译得很好,但在 CS50 编译器上,我得到了那些错误代码。我尝试使用我的代码,发现图像结果是正确的。所以我很困惑为什么我会收到这些错误。
编辑 1:
删除 void 类型函数中的 return
语句仍然没有消除错误。
编辑 2: Link to entire code on a online ide
编辑 4:clang -fsanitize=signed-integer-overflow -fsanitize=undefined -ggdb3 -O0 -Qunused-arguments -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-参数 -Wno-unused-variable -Wshadow -o filter filter.c helpers.c
我正在编译我唯一可以编译的两个文件,即 filter.c 和 helpers.c
当我查看代码时,我注意到在 void reflected()
和 void blur()
中,我在其中有两个自定义函数,它们是在它之后声明的,导致编译器假定自定义函数'不存在'。
要解决此问题,请将自定义函数移到调用它的函数之前以避免错误。在这种情况下,swap()
应该声明或放在 void reflected()
之前,与 RGBTRIPLE value()
和 `void blur()
的情况相同
我有这个错误代码:
helpers.c:56:13: warning: implicit declaration of function 'swap' is invalid in C99 [-Wimplicit-function-declaration] swap(height, width, image, i, j); ^ helpers.c:62:6: error: conflicting types for 'swap' void swap(int height, int width, RGBTRIPLE image[height][width], int row... ^ helpers.c:56:13: note: previous implicit declaration is here swap(height, width, image, i, j); ^ helpers.c:75:31: warning: implicit declaration of function 'value' is invalid in C99 [-Wimplicit-function-declaration] image_new[i][j] = value(height, width, image, i, j); ^ helpers.c:75:29: error: assigning to 'RGBTRIPLE' from incompatible type 'int' image_new[i][j] = value(height, width, image, i, j); ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ helpers.c:87:11: error: conflicting types for 'value' RGBTRIPLE value(int height, int width, RGBTRIPLE image[height][width], i... ^ helpers.c:75:31: note: previous implicit declaration is here image_new[i][j] = value(height, width, image, i, j); ^
我知道 implicit declaration of function
与没有函数原型有关,但在我的情况下我有一个。
helpers.c
#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.0);
image[i][j].rgbtRed = average;
image[i][j].rgbtGreen = average;
image[i][j].rgbtBlue = average;
}
}
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]){
for(int i = 0; i < height; i++){
for(int j = 0; j < width; j++){
RGBTRIPLE dot = image[i][j];
image[i][j].rgbtRed = max(round(0.393 * dot.rgbtRed + 0.769 * dot.rgbtGreen + 0.189 * dot.rgbtBlue));
image[i][j].rgbtGreen = max(round(0.349 * dot.rgbtRed + 0.686 * dot.rgbtGreen + 0.168 * dot.rgbtBlue));
image[i][j].rgbtBlue = max(round(0.272 * dot.rgbtRed + 0.534 * dot.rgbtGreen + 0.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(height, width, image, i, j);
}
}
return;
}
void swap(int height, int width, RGBTRIPLE image[height][width], int row, int pix){
RGBTRIPLE 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]){
RGBTRIPLE image_new[height][width];
for(int i = 0; i < height; i++){
for(int j = 0; j < width; j++){
image_new[i][j] = value(height, width, image, i, j);
}
}
for(int i = 0; i < height; i++){
for(int j = 0; j < width; j++){
image[i][j] = image_new[i][j];
}
}
return;
}
RGBTRIPLE value(int height, int width, RGBTRIPLE image[height][width], int x, int y){
int count = 0;
int red = 0;
int blue = 0;
int green = 0;
RGBTRIPLE dot;
for(int i = -1; i <= 1; i++){
for(int j = -1; j <= 1; j++){
int length = i + x;
int spread = j + y;
if((length >= 0) && (length < height) && (spread >= 0) && (spread < width)){
red += image[length][spread].rgbtRed;
blue += image[length][spread].rgbtBlue;
green += image[length][spread].rgbtGreen;
count++;
}
}
}
dot.rgbtRed = round((float)red / count);
dot.rgbtBlue = round((float)blue / count);
dot.rgbtGreen = round((float)green / count);
return dot;
}
helpers.h
#include "bmp.h"
// Convert image to grayscale
void grayscale(int height, int width, RGBTRIPLE image[height][width]);
// Check if color value is within limit
int max(int value);
// Convert image to sepia
void sepia(int height, int width, RGBTRIPLE image[height][width]);
// Reflect image horizontally
void reflect(int height, int width, RGBTRIPLE image[height][width]);
// Swapping pixel
void swap(int height, int width, RGBTRIPLE image[height][width], int row, int pix);
// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width]);
RGBTRIPLE value(int height, int width, RGBTRIPLE image[height][width], int x, int y);
bmp.h
// BMP-related data types based on Microsoft's own
#include <stdint.h>
/**
* Common Data Types
*
* The data types in this section are essentially aliases for C/C++
* primitive data types.
*
* Adapted from http://msdn.microsoft.com/en-us/library/cc230309.aspx.
* See http://en.wikipedia.org/wiki/Stdint.h for more on stdint.h.
*/
typedef uint8_t BYTE;
typedef uint32_t DWORD;
typedef int32_t LONG;
typedef uint16_t WORD;
/**
* BITMAPFILEHEADER
*
* The BITMAPFILEHEADER structure contains information about the type, size,
* and layout of a file that contains a DIB [device-independent bitmap].
*
* Adapted from http://msdn.microsoft.com/en-us/library/dd183374(VS.85).aspx.
*/
typedef struct
{
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
} __attribute__((__packed__))
BITMAPFILEHEADER;
/**
* BITMAPINFOHEADER
*
* The BITMAPINFOHEADER structure contains information about the
* dimensions and color format of a DIB [device-independent bitmap].
*
* Adapted from http://msdn.microsoft.com/en-us/library/dd183376(VS.85).aspx.
*/
typedef struct
{
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
} __attribute__((__packed__))
BITMAPINFOHEADER;
/**
* RGBTRIPLE
*
* This structure describes a color consisting of relative intensities of
* red, green, and blue.
*
* Adapted from http://msdn.microsoft.com/en-us/library/aa922590.aspx.
*/
typedef struct
{
BYTE rgbtBlue;
BYTE rgbtGreen;
BYTE rgbtRed;
} __attribute__((__packed__))
RGBTRIPLE;
我想不出为什么这是错误的。
当我这样做时,代码编译得很好,但在 CS50 编译器上,我得到了那些错误代码。我尝试使用我的代码,发现图像结果是正确的。所以我很困惑为什么我会收到这些错误。
编辑 1:
删除 void 类型函数中的 return
语句仍然没有消除错误。
编辑 2: Link to entire code on a online ide
编辑 4:clang -fsanitize=signed-integer-overflow -fsanitize=undefined -ggdb3 -O0 -Qunused-arguments -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-参数 -Wno-unused-variable -Wshadow -o filter filter.c helpers.c
我正在编译我唯一可以编译的两个文件,即 filter.c 和 helpers.c
当我查看代码时,我注意到在 void reflected()
和 void blur()
中,我在其中有两个自定义函数,它们是在它之后声明的,导致编译器假定自定义函数'不存在'。
要解决此问题,请将自定义函数移到调用它的函数之前以避免错误。在这种情况下,swap()
应该声明或放在 void reflected()
之前,与 RGBTRIPLE value()
和 `void blur()