C:“无效使用未定义的类型'结构X'&将指针解引用为不完整类型"错误 [英] C: "invalid use of undefined type ‘struct X’ & dereferencing pointer to incomplete type" errors

查看:284
本文介绍了C:“无效使用未定义的类型'结构X'&将指针解引用为不完整类型"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几天来我一直在仔细研究类似的问题,但仍然没有找到解决方案.谢谢任何帮助:

I've been perusing similar questions to mine for a couple of days but still haven't found a solution. Thanks any any help:

我有两个文件,一个包含用于处理有理数的方法,另一个包含在二维数组中处理它们的方法.我的问题是matrix.c无法识别fraction.c中包含的分数结构.我相信我的问题在某种程度上与声明2d数组的方式有关.

I have two files, one containing methods for dealing with rational numbers and one that handles them in a 2 dimensional array. My problem is matrix.c doesn't recognize the fraction structure in contained in fraction.c. I believe my problem is somehow related to the way I declared my 2d array.

infraction.c:

In fraction.c:

struct fraction {
   int integer;
   int num;
   int den;
};
typedef struct fraction* fractionRef;  //This line is in fraction.h

在matrix.c中:

In matrix.c:

#include "fraction.h"

typedef struct matrix* matrixRef;

struct matrix {
   int rows;
   int columns;
   fractionRef *m;
}matrix;

matrixRef new_matrix ( int rows, int columns ) {
   matrixRef matrix;
   matrix = (matrixRef)malloc( sizeof( matrix ) );
   matrix->m = (fractionRef*)calloc( rows, sizeof( fractionRef ) );
   int i;
   for ( i=0; i<=rows; i++ )
     matrix->m[i] = (fractionRef)calloc( columns, sizeof( fractionRef ) );
   assert( matrix->m );
   return matrix;

}
void free_matrix ( matrixRef freeMe ) {
   if ( freeMe != NULL ){
      int i, j;
      for( i = 0; i <= freeMe->rows; i++ ){
         for ( j = 0; j <= freeMe->columns; j++ ){
            free_fraction( freeMe->m[i][j] ); //ERROR OCCURS HERE
         }
      }
      freeMe->rows = 0;
      freeMe->columns = 0;
      free(freeMe);
      freeMe = NULL;
   }
}

我得到的错误与我标记的matrix.c中的行相对应.

The error I get corresponds to the line in matrix.c I marked.

matrix.c:47: error: invalid use of undefined type ‘struct fraction’
matrix.c:47: error: dereferencing pointer to incomplete type

这可能是因为我在c之前学过java,这是一个很大的错误!!!再次感谢您的帮助.

This is probably all because I learned java BEFORE c, big mistake!!! Thanks again for any help.

谢谢大家.因此,我现在所看到的是头文件.h文件中的所有内容都与java中的public类似.我的分数结构定义不是对c编译器公开"的,因此我的matrix.c无法访问它.

Thanks everyone. So the way I see it now is everything in the header .h files are similar to public in java. My fraction struct definition wasn't "public" to the c compiler so my matrix.c wasn't able to access it.

推荐答案

您必须将struct的定义移到fraction.h文件中.与Java不同,编译器和链接器不会神奇地"使一个.c文件引用另一个.c文件中的信息. (如果您在另一个文件中#include一个.c文件,这可能是个好主意.)

You must move the definition of the struct into the fraction.h file. Unlike Java, the compiler and linker do not "magically" cause one .c file to reference information inside another .c file. (It could be done if you #include one .c file in another -- a bad idea.)

指令

#include"fraction.h"

#include "fraction.h"

导致将头文件的文本内容放置在该指令所在的行上,就像通过剪切和粘贴一样.编译器一次处理一个输入文件,读入#include个文件,并且在编译一个.c文件时必须提供所有必需的信息.

causes the text contents of the header file to be placed, as if by cut-and-paste, at the line with that directive. The compiler processes one input file at a time, reading in the #include'd files, and all the needed information must be present while that one .c file is compiled.

为了帮助您理解,我将指出一种糟糕的方法来满足您的要求:只需将struct fraction的结构定义剪切并粘贴到matrix.c的顶部,就在#include "fraction.h"之前- -结果将编译.在Java中,编译器可能会抱怨您声明了某些重复类型.在C语言中,您要做的是定义两个不同的结构,它们碰巧具有相同的内存布局和相同的名称.从将目标文件链接在一起的角度来看,只需使用相同的内存布局即可使它们互换.

To help with your understanding, I will point out a terrible way to accomplish what you require: simply cut-and-paste the struct definition of struct fraction into the top of matrix.c, immediately prior to #include "fraction.h" -- the result will compile. In Java, the compiler might complain that you have declared some duplicate types. In C, what you've done is define two different structs, which happen to have an identical memory layout and the same name. Only the same memory layout is needed to make them interchangable from the perspective of linking together object files.

是的,这是您对Java的误解.您正在学习C真是太好了!

Yes, this is a misunderstanding you picked up from Java. It's wonderful that you are learning C!

这篇关于C:“无效使用未定义的类型'结构X'&amp;将指针解引用为不完整类型"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆