2012-04-28 25 views
0

我正在編碼一個矩陣,其條目是具有有理係數的多項式。任何幫助將不勝感激。
我有合理的數量和合理的多項式聲明:
rational_number.h合理多項式數組的編譯錯誤

struct long_rational{ 
    long p; 
    long q; 
    }; 

typedef struct long_rational rational; 

polynomial.h

#define MAX_DEGREE 200 

struct rational_polynomial{ 
    long degree; 
    rational coef[MAX_DEGREE]; //rational coefficients in increase power. 
}; 

typedef struct rational_polynomial polynomial; 

poly_mat.c的全部

#include "poly_mat.h" 

#define NR_END 1 
#define FREE_ARG char* 

polynomial **poly_matrix(long nrl, long nrh, long ncl, long nch) 
/* allocates a matrix with polynomial entries in the range m[nrl..nrh][ncl..nch] */ 
{ 
    long i, nrow=nrh-nrl+1,ncol=nch-ncl+1; 
    polynomial **m; 
    /* allocate pointers to rows */ 
    m=(polynomial **) malloc((size_t)((nrow+NR_END)*sizeof(polynomial*))); 
    if (!m) nrerror("allocation failure 1 in matrix()"); 
    m += NR_END; 
    m -= nrl; 
    /* allocate rows and set pointers to them */ 
    m[nrl]=(polynomial *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(polynomial))); 
    if (!m[nrl]) nrerror("allocation failure 2 in matrix()"); 
    m[nrl] += NR_END; 
    m[nrl] -= ncl; 
    for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol; 
    /* return pointer to array of pointers to rows */ 
    return m; 
} 

void **free_poly_matrix(polynomial **m, long nrl, long nrh, long ncl, long nch) 
/* free a polynomial matrix allocated by poly_matrix() */ 
{ 
    free((FREE_ARG) (m[nrl]+ncl-NR_END)); 
    free((FREE_ARG) (m+nrl-NR_END)); 
} 

void init_random_poly_matrix(int **m, long nrl, long nrh, long ncl, long nch) 
/* initialize a random polynomial matrix with coefficient <=100*/ 
{ 
    long i,j; 
    long iseed = (long)time(NULL); 
    srand (iseed); 
    for (i=nrl; i<=nrh; i++) 
    { 
     for (j=ncl; j<=nch; j++) 
     { 
      m[i][j].degree=(rand()%MAX_DEGREE); 
      for (k=0;k<=MAX_DEGREE;k++) 
      { 
       m[i][j].coef[k].p = (rand()%100); 
       m[i][j].coef[k].q = (1+rand()%100); 
      } 
     } 
    } 
} 

這裏是神祕的錯誤消息:

 
gcc -Wall -c -o poly_mat.o poly_mat.c 
poly_mat.c: In function ‘init_random_poly_matrix’: 
poly_mat.c:6: error: expected declaration specifiers before ‘(’ token 
poly_mat.c:28: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token 
poly_mat.c:35: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token 
poly_mat.h:14: error: old-style parameter declarations in prototyped function definition 
poly_mat.c:51: error: expected ‘{’ at end of input 
make: *** [poly_mat.o] Error 1 

poly_mat.h缺少分號填充。

#ifndef POLY_MAT_H 
#define POLY_MAT_H 

#include <math.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 
#include "nrutil.h" 
#include "rational_number.h" 
#include "polynomial.h" 

/* matrix with polynomial entries */ 
polynomial **poly_matrix(long nrl, long nrh, long ncl, long nch); 
void init_random_poly_matrix(int **m, long nrl, long nrh, long ncl, long nch); 
void **free_poly_matrix(polynomial **m, long nrl, long nrh, long ncl, long nch); 

#endif 

現在我不能使用點運算符訪問數組中的多項式成員。
新的錯誤消息:

 
gcc -Wall -c -o poly_mat.o poly_mat.c 
poly_mat.c: In function ‘init_random_poly_matrix’: 
poly_mat.c:43: error: request for member ‘degree’ in something not a structure or union 
poly_mat.c:46: error: request for member ‘coef’ in something not a structure or union 
poly_mat.c:47: error: request for member ‘coef’ in something not a structure or union 
make: *** [poly_mat.o] Error 1 

編輯2:實測值的錯誤。將其聲明爲int **而不是多項式**。

+0

什麼是poly_mat.h? – 2012-04-28 08:28:26

+0

你確定'polynomial'在.cpp文件中是可見的嗎?而且它實際上是那個時候的typedef? – 2012-04-28 08:48:19

+0

plz show us poly_mat.h – 2012-04-28 09:43:58

回答

0

不知道poly_mat.h,但我groank errormessage,poly_mat.h中有一個語法錯誤。我相信在聲明init_random_poly_matrix()的原型時,在第14行或之前缺少括號/大括號或半角符號。

+0

太棒了。發現錯誤而不看到頭文件。 – user103500 2012-04-29 05:25:45