I considered similar questions for my couple of days, but still have not found a solution. Thanks for any help:
I have two files, one of which contains methods for working with rational numbers and one that processes them in a two-dimensional array. My problem is that matrix.c does not recognize the fraction structure contained in the .c fraction. I believe that my problem is somehow related to the way I declared my 2d array.
In fraction.c:
struct fraction { int integer; int num; int den; }; typedef struct fraction* fractionRef;
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; } }
The error I am getting matches the row in matrix.c I am flagged.
matrix.c:47: error: invalid use of undefined type 'struct fraction' matrix.c:47: error: dereferencing pointer to incomplete type
This is probably all because I learned java BEFORE C, a big mistake !!! Thanks again for any help.
EDIT: Thanks to everyone. So now I see it all in the header .h files are similar to public in java. The definition of my fractional structure was not "publicly available" for the c compiler, so my .c matrix was not able to access it.
source share