I have this error code:
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); ^
I understand that implicit declaration of function
is linked with not having a function prototype but in my case i do have one.
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;
I can not think of any way of why this would be wrong.
The code compiles perfectly fine when I do it but on the CS50 compiler, I get those error codes. I tried using my code and saw that the image outcome were coming out right. So I am confused to why am i receiving those errors.
Edit 1: Deleting the return
statement in void type functions still does not remove the error.
Edit 2: Link to entire code on a online ide
Edit 4: clang -fsanitize=signed-integer-overflow -fsanitize=undefined -ggdb3 -O0 -Qunused-arguments -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow -o filter filter.c helpers.c
I am compiling the only two files i can compile which is filter.c and helpers.c
As I looked through the code, I noticed in void reflected()
and void blur()
, I have two custom functions inside of it that were declared after it resulting in the compiler to assume that the custom functions 'don't exist'.
To fix the problem, move the custom function before the function it is called in to avoid the errors. In this case, swap()
should be declared or put before void reflected()
and same case with RGBTRIPLE value()
and `void blur()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.