1

当我尝试编译以下代码时,出现以下错误:

hmm.cpp:16:29: 错误: 'double gamma [3000][4]' 重新声明为不同类型的符号 /usr/include/x86_64-linux-gnu/bits/mathcalls.h:266:1: 错误: 以前>'double gamma(double)' hmm.cpp 的声明:在函数'double updateModel(int&, int, int, double, double, int, double*, >double ( )[4], double ( )[5005],双倍的*)':

hmm.cpp:67:11:警告:指向算术中使用的函数的指针 [-Wpointer-arith] hmm.cpp:67:14:警告:指向算术中使用的函数的指针 [-Wpointer-arith] hmm.cpp: 67:18:错误:分配函数“双伽马(双)”hmm.cpp:67:18:错误:无法在分配中将“int”转换为“双(双)throw()”

hmm.cpp:69:12:警告:指向算术中使用的函数的指针 [-Wpointer-arith] hmm.cpp:69:15:警告:指向算术中使用的函数的指针 [-Wpointer-arith] hmm.cpp: 69:46:错误:'double(double)throw ()' 和 'double' 类型的无效操作数到 >binary 'operator+'</p>

hmm.cpp:69:46: 错误:在评估 'operator+=(double(double)throw (), double)'</p>

每次在代码中使用 gamma 时,我都会遇到类似的错误。代码如下:

#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdlib>
#include <cmath>
//double atof(const char* str)
using namespace std;
#define MAXT 3000
#define MAXSTATE 4
#define MAXRANGE 5005
#define maxString 52
#define maxResult 405
double alpha[MAXT][MAXSTATE];
double beta[MAXT][MAXSTATE];

double gamma [MAXT][MAXSTATE];

double delta[MAXT][MAXSTATE];
double psi[MAXT][MAXSTATE];//Ψ
double xi[MAXT][MAXSTATE][MAXSTATE];
inline int getIndex(const double& value,const double& min,const double& 
    max,const int& k)
{
    int ret;
//ret = int((value - min)*((max-min)/k)); // [possible error 1]
    ret = (k - 1)*(value - min) / (max-min);
    return ret;
}
// all the matrix start from 1 to max
// oMin is the minimal value of O
double updateModel(int& q,int tWindow, int oRange, double oMin, double oMax, int
    stateNum, double _o[MAXT],double _A[MAXSTATE][MAXSTATE],double _B[MAXSTATE][MAXRANGE],double _Pi[MAXSTATE])
{
    double p;
/* calculate lambda */
// alpha
    for(int s=1;s<=stateNum;s++)
        alpha[1][s] = _Pi[s]*_B[s][getIndex(_o[1], oMin, oMax, oRange)];
    for(int t=2;t<=tWindow;t++)
    {
        for(int s=1;s<=stateNum;s++)
        {
            alpha[t][s] = 0;
            for(int j=1;j<=stateNum;j++)
                alpha[t][s] += alpha[t-1][j] * _A[j][s] * _B[s][getIndex(_o[t], oMin, oMax, oRange)];
        }
    }
// p
    p = 0;
    for(int i=1;i<=stateNum;i++)
        p+=alpha[tWindow][i];
//beta
    for(int s = 1; s <= stateNum; s++)
        beta[tWindow][s] = 1;
    for(int t = tWindow - 1; t >= 1; t--)
    {
        for(int s = 1; s <= stateNum; s++)
        {
            beta[t][s] = 0;
            for(int j=1;j<=stateNum;j++)
                beta[t][s] += beta[t + 1][j] * _A[j][s] * _B[s][getIndex(_o[t + 1], oMin, oMax, oRange)];
        }
    }
//gamma
    for (int t = 1; t <= tWindow; t ++){
        for (int i = 1; i <= stateNum; i ++){
            gamma[t][i] = 0;
            for (int s = 1; s <= stateNum; s ++){
                gamma[t][i] +=  (alpha[t][s] * beta[t][s]);
            }
            gamma[t][i] = alpha[t][i] * beta[t][i] / gamma[t][i];
        }
    }
//delta, psi
    for (int i = 1; i <= stateNum; i ++){
        delta[1][i] = _Pi[i] * _B[i][getIndex(_o[1], oMin, oMax, oRange)];
        psi[1][i] = 0;
    }
    for (int t = 2; t <= tWindow; t ++){
        for (int i = 1; i <= stateNum; i ++){
            int k = 1;
            delta[t][1] = delta[t - 1][1] * _A[1][i] * _B[i][getIndex(_o[t], oMin, oMax, oRange)];
            for (int j = 2; j <= stateNum; j ++)
            {
                if ((delta[t - 1][j] * _A[j][i]) > (delta[t - 1][k] * 
                    _A[k][i]) )
                {
                    delta[t][i] = delta[t - 1][j] * _A[j][i] * 
                    _B[i][getIndex(_o[t], oMin, oMax, oRange)];
                    k = j;
                }
            }
            psi[t][i] = k; 
        }
    }
    int k = 1;
    double p_star = delta[tWindow][1];
    for (int i = 1; i <= stateNum - 1; i ++)
    {
        if (delta[tWindow][i + 1] > delta[tWindow][k])
        {
            p_star = delta[tWindow][i + 1];
            k = i + 1;
        }
    }
    int q_star = k; 
//xi
    for (int t = 1; t <= tWindow - 1; t ++)
    {
        for (int i = 1; i <= stateNum; i ++)
        {
            for (int j = 1; j <= stateNum; j ++)
            {
                xi[t][i][j] = 0;
                for (int s1 = 1; s1 <= stateNum; s1 ++)
                {
                    for (int s2 = 1; s2 <= stateNum; s2 ++)
                    {
                        xi[t][i][j] = xi[t][i][j] + beta[t + 1][s2] 
                        * _B[s2][getIndex(_o[t + 1], oMin, oMax, oRange)] * _A[s1][s2] * alpha [t][s1];
                    }
                }
                xi[t][i][j] = beta[t + 1][j] * _B[j][getIndex(_o[t + 1], 
                    oMin, oMax, oRange)] * _A[i][j] * alpha [t][i] / xi[t][i][j];
            }
        }
    }
//update
    for (int i = 1; i <= stateNum; i ++)
    {
        _Pi[i] = gamma[1][i];
        for (int j = 1; j <= stateNum; j ++)
        {
            double numerator = 0;
            double denominator = 0;
            for (int t = 1; t <= tWindow - 1; t ++)
            {
                numerator += xi[t][i][j];
                denominator += gamma[t][i];
            }
            _A[i][j] = numerator / denominator;
        }
        double tmp,detmp;
        for(int k=1; k<=oRange; k++)
        {
            tmp = 0;
            detmp = 0;
            for(int t=1; t<=tWindow; t++)
            {
                if(getIndex(_o[t], oMin, oMax, oRange) == k ) tmp+=gamma[t][i];
                detmp+=gamma[t][i];
            }
            _B[i][k] = tmp/detmp;
        }
    }
    q = q_star;
    return p;
}
//double _A[maxState][maxState],double _B[maxState][MAXRANGE],double _Pi[maxState]
void converge(int& q, double previousP,double threshold, int tWindow, int
    maxRange, double oMin, double oMax, int stateNum, double _o[MAXT],double _A[MAXSTATE][MAXSTATE],double _B[MAXSTATE][MAXRANGE],double _Pi[MAXSTATE])
{
    double currentP = updateModel(q, tWindow,maxRange,oMin,oMax,stateNum, _o, 
        _A,_B,_Pi);
    while(fabs(currentP-previousP)>threshold)
    {
        previousP = currentP;
        currentP = updateModel(q, tWindow,maxRange,oMin,oMax,stateNum, _o, 
            _A,_B,_Pi);
    }
}
int main()
{
    ifstream fin1("..\\data\\input.txt");
    ifstream fin2("..\\data\\input2.txt");
    ofstream fout("..\\data\\output.txt");
    double result[maxResult];
    double _o[MAXT];
    double _A[MAXSTATE][MAXSTATE];
    double _B[MAXSTATE][MAXRANGE];
    double _Pi[MAXSTATE];
    int oRange;
    int nState;
    double oMin;
    double oMax;
    int tWindow;
/*
 #####################################################################
 Begin- Input data
 */
 string tnum;
 char tmps[maxString];
 double t;
 int cnt1, cnt2;
 int cnttmp;
/* Get the num of input1 and input2 */
 if(!fin1.eof()) 
 {
    getline(fin1,tnum);
    strcpy(tmps,tnum.c_str());
    t = atof(tmps);
    cnt1 = int(t);
 }
 if(!fin2.eof()) 
 {
    getline(fin2,tnum);
    strcpy(tmps,tnum.c_str());
    t = atof(tmps);
    cnt2 = int(t);
 }
/* Get the real data of input1 and input2 */
 cnttmp = 1;
 oMin = oMax = 0;
 while(!fin1.eof())
 {
    getline(fin1,tnum);
    strcpy(tmps,tnum.c_str());
    t = atof(tmps);
    _o[cnttmp++] = t;
    if(oMin > t) oMin = t;
    if(oMax < t) oMax = t;
// printf("1: %lf\n",t);
 }
//printf("oMin = %lf, oMax = %lf\n",oMin, oMax);
 while(!fin2.eof())
 {
    getline(fin2,tnum);
    strcpy(tmps,tnum.c_str());
    t = atof(tmps);
    _o[cnttmp++] = t;
//printf("2: %lf\n",t);
 }
/*
 End- Input data
 #####################################################################
 */
/*
 Parameters to set:
 int oRange;
 int tWindow;
 */
 int maxRange = 5000;
 tWindow = 70;
 nState = 3;
 double previousP = 0;
 double threshold = 1e-8;
// [To do]
 for(int i=1;i<=nState;i++)
    for(int j=1;j<=nState;j++)
        _A[i][j] = (1.0)/ nState;
    for(int i=1;i<=nState;i++)
        for(int j=1;j<=maxRange;j++)
            _B[i][j] = (1.0)/maxRange;
        for(int i=1;i<=nState;i++)
            _Pi[i] = (1.0)/nState;
/*
 #####################################################################
 Begin- Process data
 */
 int q_star;
 converge(q_star,previousP,threshold, tWindow, maxRange, oMin, oMax, 3, 
    _o,_A,_B,_Pi);
int bestIndex = 1; // the index of O(T+1)
int tmp;
int choice;
double predictValue,currentValue;
double bestValue;
for(int k=1;k<=cnt2;k++) // cnt2 Real Data
{
    currentValue = _o[cnt1+k-1];
    bestValue = 0;
    for(int i=1;i<=maxRange;i++)
    {
//tmp = getIndex(_o[cnt1+k], oMin, oMax, maxRange);
        if(_B[q_star][i] > bestValue)
        {
            bestValue = _B[q_star][i];
            bestIndex = i;
        }
    }
    predictValue = oMin + (oMax - oMin) * (bestIndex-1) /(maxRange-1); 
//index --> value
    converge(q_star,previousP,threshold, tWindow, maxRange, oMin, oMax, 
        3, _o+k,_A,_B,_Pi);
    if(predictValue > currentValue) choice = 1;
    else choice = -1;
    result[k] = choice * (_o[cnt1+k] - _o[cnt1+k-1]);
}
/*
 End- Process data
 #####################################################################
 */
/*
 #####################################################################
 Begin- Output data
 */
 for(int i=1;i<=cnt2;i++)
    fout << result[i] << endl;
/*
 End- Output data
 #####################################################################
 */
 fin1.close();
 fin2.close();
 fout.close();
 return 0;
}

有人能告诉我如何解决这个错误吗?谢谢你。

4

2 回答 2

3

错误信息很清楚:

mathcalls.h:266:1: 错误: >'double gamma(double)' 的先前声明

double gamma(double)导入时有一个功能cmath

更改阵列的名称。

于 2013-04-30T01:58:42.897 回答
2

您的变量gamma与 mathcalls.h 中定义的符号冲突,这是 gamma 函数的原型。

于 2013-04-30T02:01:56.333 回答