我几天来一直在仔细研究与我类似的问题,但仍然没有找到解决方案。感谢任何帮助:
我有两个文件,一个包含处理有理数的方法,另一个包含处理二维数组的方法。我的问题是matrix.c 无法识别fraction.c 中包含的分数结构。我相信我的问题在某种程度上与我声明二维数组的方式有关。
在fraction.c中:
struct fraction {
int integer;
int num;
int den;
};
typedef struct fraction* fractionRef; //This line is in fraction.h
在矩阵.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 中的行。
matrix.c:47: error: invalid use of undefined type ‘struct fraction’
matrix.c:47: error: dereferencing pointer to incomplete type
这可能都是因为我先学java再学c,大错特错!!!再次感谢任何帮助。
编辑:谢谢大家。所以我现在看到的方式是头 .h 文件中的所有内容都类似于 java 中的 public 。我的分数结构定义对于 c 编译器来说不是“公开的”,因此我的 matrix.c 无法访问它。