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

时间:2021-03-19 08:34:55

标签: c function struct header bmp

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);

头文件:

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

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

主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);
}

错误仅针对 writePixelsBMP 和 readPixelsBMP 显示,我尝试在头文件、主文件、源文件的开头声明它们,但没有任何效果。我很困惑,请帮忙。另外,如果您有时间,请检查我的 readPixelsBMP 是否可以完成从 bmp 文件读取像素的工作。谢谢。

错误信息:

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

1 个答案:

答案 0 :(得分:1)

在处理编译器错误时,应该自上而下解决它们并包含警告。我们看到的第一个警告是:

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 中没有可见的 struct Pixel 定义,导致 struct Pixel 被声明为参数列表的一部分。在这种情况下,声明的范围仅在声明本身。

然后当在 BmpProcessor.c 中看到这个函数的定义时,确实看到了 struct Pixel 的声明,你最终会得到两个不同的同名结构声明,并且因为结构不同,你会得到关于类型冲突的错误。

由于 BmpProcessor.h 依赖于 PixelProcessor.h 中的结构体定义,因此您需要在 BmpProcessor.h 中#include "PixelProcessor.h"