C 有理多项式数组的编译错误

C 有理多项式数组的编译错误,c,struct,polynomial-math,C,Struct,Polynomial Math,我正在编写一个矩阵,它的条目是有理系数多项式。任何帮助都将不胜感激。 我声明了有理数和有理多项式: 有理数 struct long_rational{ long p; long q; }; typedef struct long_rational rational; 多项式h #define MAX_DEGREE 200 struct rational_polynomial{ long degree; rational coef[MAX_DEGREE

我正在编写一个矩阵,它的条目是有理系数多项式。任何帮助都将不胜感激。 我声明了有理数和有理多项式: 有理数

struct long_rational{
    long p;
    long q;
    };

typedef struct long_rational rational;
多项式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**而不是多项式**。

在poly_mat.h中不知道,但我发现错误消息,poly_mat.h中有语法错误。我认为在声明init_random_poly_matrix的原型时,第14行或之前缺少了括号/括号或semikolon。

您确定多项式在.cpp文件中可见吗?在这一点上它实际上是typedef吗?这段代码中似乎有很多小错误,它们会阻止编译,或者至少会发出编译器警告,这表明您设计了代码,并在没有测试任何代码的情况下全部输入了代码。从小处开始,在每个阶段进行构建和测试,永远不要添加不起作用的代码。太棒了。在没有看到头文件的情况下发现错误。