简体   繁体   English

function 的类型冲突错误(C 编程)

[英]Conflicting types error for a function (C Programming)

C source file: C 源文件:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

#include "BmpProcessor.h"
#include "PixelProcessor.h"


/**
 * read BMP header of a file. Useful for converting files from PPM to BMP.
 *
 * @param  file: A pointer to the file being read or written
 * @param  header: Pointer to the destination BMP header
 */
void readBMPHeader(FILE* file, struct BMP_Header* header){
    fread(&header->sign_ID, sizeof(char)*2, 1, file);
    fread(&header->size, sizeof(int), 1, file);
    fread(&header->reserved1, sizeof(short), 1, file);
    fread(&header->reserved2, sizeof(short), 1, file);
    fread(&header->pix_Array, sizeof(int), 1, file);
}

/**
 * write BMP header of a file. Useful for converting files from PPM to BMP.
 *
 * @param  file: A pointer to the file being read or written
 * @param  header: The header made by makeBMPHeader function
 */
void writeBMPHeader(FILE* file, struct BMP_Header* header);

/**
 * read DIB header from a file. Useful for converting files from PPM to BMP.
 *
 * @param  file: A pointer to the file being read or written
 * @param  header: Pointer to the destination DIB header
 */
void readDIBHeader(FILE* file, struct DIB_Header* header){
    fread(&header->size, sizeof(struct DIB_Header), 1, file);
}

/**
 * write DIB header of a file. Useful for converting files from PPM to BMP.
 *
 * @param  file: A pointer to the file being read or written
 * @param  header: The header made by makeDIBHeader function
 */
void writeDIBHeader(FILE* file, struct DIB_Header* header);

/**
 * make BMP header based on width and height. 
 * The purpose of this is to create a new BMPHeader struct using the information 
 * from a PPMHeader when converting from PPM to BMP.
 *
 * @param  header: Pointer to the destination DIB header
 * @param  width: Width of the image that this header is for
 * @param  height: Height of the image that this header is for
 */
void makeBMPHeader(struct BMP_Header* header, int width, int height);


 /**
 * Makes new DIB header based on width and height. Useful for converting files from PPM to BMP.
 *
 * @param  header: Pointer to the destination DIB header
 * @param  width: Width of the image that this header is for
 * @param  height: Height of the image that this header is for
 */
void makeDIBHeader(struct DIB_Header* header, int width, int height);


/**
 * read Pixels from BMP file based on width and height.
 *
 * @param  file: A pointer to the file being read or written
 * @param  pArr: Pixel array of the image that this header is for
 * @param  width: Width of the image that this header is for
 * @param  height: Height of the image that this header is for
 */
void readPixelsBMP(FILE* file, struct Pixel** pArr, int width, int height){
pArr = (struct pArr**)malloc(height*sizeof(void*));
for( int i = height-1; i = 0; i++){
    pArr[i] = (struct pArr*)malloc(width*sizeof(struct Pixel));
    fread(pArr[i],width,sizeof(struct Pixel),file);
    }
}

/**
 * write Pixels from BMP file based on width and height.
 *
 * @param  file: A pointer to the file being read or written
 * @param  pArr: Pixel array of the image that this header is for
 * @param  width: Width of the image that this header is for
 * @param  height: Height of the image that this header is for
 */
void writePixelsBMP(FILE* file, struct Pixel** pArr, int width, int height);

Header file: Header 文件:

#include <stdio.h>
#include <stdlib.h>

struct Pixel{
    unsigned char red;
    unsigned char rgbgreen;
    unsigned char rgbblue;
};

Main c file:主要 c 文件:

#include "BmpProcessor.h"
#include "PixelProcessor.h"

struct BMP_Header *header;
struct DIB_Header *DIBheader;
struct Pixel **pArr;
FILE *file;

/*
 * 
 */
int main() {
    file = fopen("test2.bmp","rb");
    readBMPHeader(file, header);
    readDIBHeader(file, DIBheader);
    readPixelsBMP(file, pArr, DIBheader->img_width, DIBheader->img_height);
    fclose(file);
    return (EXIT_SUCCESS);
}

The error is shown for writePixelsBMP and readPixelsBMP only and I have tried declaring them at the start of the header file, main file, source file but nothing works.该错误仅针对 writePixelsBMP 和 readPixelsBMP 显示,我尝试在 header 文件、主文件、源文件的开头声明它们,但没有任何效果。 I am very confused, please help.我很困惑,请帮助。 Also, if you have time please check if my readPixelsBMP would do its job in reading pixels from a bmp file.另外,如果您有时间,请检查我的 readPixelsBMP 是否可以从 bmp 文件中读取像素。 Thank you.谢谢你。

Error message:错误信息:

cd '/home/lot/NetBeansProjects/ser334'
/usr/bin/make -f Makefile CONF=Debug
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory '/home/daryl/NetBeansProjects/ser334'
"/usr/bin/make"  -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux/ser334
make[2]: Entering directory '/home/daryl/NetBeansProjects/ser334'
mkdir -p build/Debug/GNU-Linux
rm -f "build/Debug/GNU-Linux/BmpProcessor.o.d"
gcc    -c -g -MMD -MP -MF "build/Debug/GNU-Linux/BmpProcessor.o.d" -o build/Debug/GNU-Linux/BmpProcessor.o BmpProcessor.c
In file included from BmpProcessor.c:7:0:
BmpProcessor.h:93:39: warning: ‘struct Pixel’ declared inside parameter list will not be visible outside of this definition or declaration
 void readPixelsBMP(FILE* file, struct Pixel** pArr, int width, int height);
                                       ^~~~~
BmpProcessor.h:104:40: warning: ‘struct Pixel’ declared inside parameter list will not be visible outside of this definition or declaration
 void writePixelsBMP(FILE* file, struct Pixel** pArr, int width, int height);
                                        ^~~~~
BmpProcessor.c:81:6: error: conflicting types for ‘readPixelsBMP’
 void readPixelsBMP(FILE* file, struct Pixel** pArr, int width, int height){
      ^~~~~~~~~~~~~
In file included from BmpProcessor.c:7:0:
BmpProcessor.h:93:6: note: previous declaration of ‘readPixelsBMP’ was here
 void readPixelsBMP(FILE* file, struct Pixel** pArr, int width, int height);
      ^~~~~~~~~~~~~
BmpProcessor.c: In function ‘readPixelsBMP’:
BmpProcessor.c:82:6: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
 pArr = (struct pArr**)malloc(height*sizeof(void*));
      ^
BmpProcessor.c:84:13: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
     pArr[i] = (struct pArr*)malloc(width*sizeof(struct Pixel));
             ^
BmpProcessor.c: At top level:
BmpProcessor.c:97:6: error: conflicting types for ‘writePixelsBMP’
 void writePixelsBMP(FILE* file, struct Pixel** pArr, int width, int height);
      ^~~~~~~~~~~~~~
In file included from BmpProcessor.c:7:0:
BmpProcessor.h:104:6: note: previous declaration of ‘writePixelsBMP’ was here
 void writePixelsBMP(FILE* file, struct Pixel** pArr, int width, int height);
      ^~~~~~~~~~~~~~
nbproject/Makefile-Debug.mk:67: recipe for target 'build/Debug/GNU-Linux/BmpProcessor.o' failed
make[2]: *** [build/Debug/GNU-Linux/BmpProcessor.o] Error 1
make[2]: Leaving directory '/home/daryl/NetBeansProjects/ser334'
nbproject/Makefile-Debug.mk:60: recipe for target '.build-conf' failed
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory '/home/daryl/NetBeansProjects/ser334'
nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' failed
make: *** [.build-impl] Error 2

When handling compiler errors, they should be addressed from the top down and include warnings.处理编译器错误时,应自上而下解决并包括警告。 The first warning we see is:我们看到的第一个警告是:

BmpProcessor.h:93:39: warning: ‘struct Pixel’ declared inside parameter list will not be visible outside of this definition or declaration
 void readPixelsBMP(FILE* file, struct Pixel** pArr, int width, int height);
                                       ^~~~~

This indicates that there's no definition for struct Pixel visible inside of BmpProcessor.h, resulting in struct Pixel being declared as part of the parameter list.这表明 BmpProcessor.h 中没有可见的struct Pixel定义,导致struct Pixel被声明为参数列表的一部分。 In such a case, the scope of the declaration is only in the declaration itself.在这种情况下,声明的 scope 仅在声明本身中。

Then later when the definition of this function is seen in BmpProcessor.c which does see the declaration of struct Pixel , you end up with two different struct declarations with the same name, and because the structs are different you get the error about conflicting types.然后稍后当在 BmpProcessor.c 中看到这个 function 的定义时,它确实看到了struct Pixel的声明,你最终得到两个具有相同名称的不同结构声明,并且由于结构不同,你会得到关于冲突类型的错误。

Since BmpProcessor.h depends on the struct definition in PixelProcessor.h, you need to #include "PixelProcessor.h" inside of BmpProcessor.h.由于 BmpProcessor.h 依赖于 PixelProcessor.h 中的结构定义,因此您需要在 BmpProcessor.h 中#include "PixelProcessor.h"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM