2013-04-30 27 views
1

当我尝试编译下面的代码,我收到以下错误:C++编译错误:“双阵列重新声明为不同意义的符号”

hmm.cpp:16:29: error: ‘double gamma [3000][4]’ redeclared as different kind of symbol /usr/include/x86_64-linux-gnu/bits/mathcalls.h:266:1: error: previous declaration of >‘double gamma(double)’ hmm.cpp: In function ‘double updateModel(int&, int, int, double, double, int, double*, >double ()[4], double ()[5005], double*)’:

hmm.cpp:67:11: warning: pointer to a function used in arithmetic [-Wpointer-arith] hmm.cpp:67:14: warning: pointer to a function used in arithmetic [-Wpointer-arith] hmm.cpp:67:18: error: assignment of function ‘double gamma(double)’ hmm.cpp:67:18: error: cannot convert ‘int’ to ‘double(double)throw()’ in assignment

hmm.cpp:69:12: warning: pointer to a function used in arithmetic [-Wpointer-arith] hmm.cpp:69:15: warning: pointer to a function used in arithmetic [-Wpointer-arith] hmm.cpp:69:46: error: invalid operands of types ‘double(double)throw()’ and ‘double’ to >binary ‘operator+’

hmm.cpp:69:46: error: in evaluation of ‘operator+=(double(double)throw(), double)’

我得到类似的错误,每次伽马在代码中使用。 代码如下:

#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; 
} 

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

+0

如果您在引用的头文件中查找错误:241:#if defined __USE_MISC ||定义__USE_XOPEN 242:/ *'lgamma'的过时别名。 */ 243:__MATHCALL(gamma ,,(_Mdouble_)); 244:#endif 245: – OldProgrammer 2013-04-30 01:58:23

+0

你介意只给我们看相关的代码吗? – 0x499602D2 2013-04-30 02:01:04

回答

3

的错误信息是很清楚的:

mathcalls.h:266:1: error: previous declaration of >‘double gamma(double)’

有一个功能​​你导入cmath时得到的。

更改数组的名称。

+0

就个人而言,我总是用'g_'前缀我的全局变量以避免这样的错误。另外它避免了局部变量命名混淆。 – ApproachingDarknessFish 2013-04-30 02:05:25

2

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