C++ C++;编译错误:";重新声明为不同类型符号的双数组“;
当我尝试编译以下代码时,会出现以下错误: 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],double*)”中: hmm.cpp:67:11:警告:指向算术中使用的函数的指针[-Wpointer arith] hmm.cpp:67:14:警告:指向算术中使用的函数的指针[-Wpointer arith] hmm.cpp:67:18:错误:函数'double gamma(double)'的赋值 hmm.cpp:67:18:错误:无法将赋值中的“int”转换为“double(double)throw()” hmm.cpp:69:12:警告:指向算术中使用的函数的指针[-Wpointer arith] hmm.cpp:69:15:警告:指向算术中使用的函数的指针[-Wpointer arith] hmm.cpp:69:46:错误:“double(double)throw()”和“double”to>binary“operator+”类型的操作数无效 hmm.cpp:69:46:错误:在计算“运算符+=(双(双)掷(),双)”时 每次代码中使用gamma时,我都会遇到类似的错误。 代码如下:C++ C++;编译错误:";重新声明为不同类型符号的双数组“;,c++,C++,当我尝试编译以下代码时,会出现以下错误: 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()
#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;
}
#包括
#包括
#包括
#包括
#包括
//双atof(常量字符*str)
使用名称空间std;
#定义MAXT 3000
#定义MAXSTATE 4
#定义最大范围5005
#定义maxString 52
#定义maxResult 405
双alpha[MAXT][MAXSTATE];
双beta[MAXT][MAXSTATE];
双伽马[MAXT][MAXSTATE];
双增量[MAXT][MAXSTATE];
双磅/平方英寸[MAXT][MAXSTATE]//Ψ
双十一[MAXT][MAXSTATE][MAXSTATE];
内联int getIndex(常数双精度和值、常数双精度和最小值、常数双精度和
最大值,常数(整数和整数)
{
int ret;
//ret=int((value-min)*((max-min)/k));//[可能的错误1]
ret=(k-1)*(值-最小值)/(最大-最小值);
返回ret;
}
//所有矩阵从1开始到最大值
//oMin是O的最小值
双更新模型(int&q、int-tWindow、int-oRange、双oMin、双oMax、int
stateNum、双o[MAXT]、双A[MAXSTATE][MAXSTATE]、双B[MAXSTATE][MAXRANGE]、双Pi[MAXSTATE])
{
双p;
/*计算λ*/
//阿尔法
对于(int s=1;s而言,错误信息非常清楚:
mathcalls.h:266:1:错误:先前声明的>'double gamma(double)'
导入cmath
时,会得到一个函数double gamma(double)
更改阵列的名称。错误消息非常清楚:
mathcalls.h:266:1:错误:先前声明的>'double gamma(double)'
导入cmath
时,会得到一个函数double gamma(double)
更改数组的名称。您的变量gamma
与mathcalls.h中定义的符号冲突,后者是gamma函数的原型。您的变量gamma
与mathcalls.h中定义的符号冲突,后者是gamma函数的原型。如果在引用的头文件中查找错误:241:#如果已定义的u-USE_u-MISC | |已定义的u-USE_u-XOPEN 242:/*用于'lgama'的过时别名。*/243:u-MATHCALL(gamma,,(_-Mdouble));244:#endif 245:您介意只向我们显示相关代码吗?如果您查看引用的头文件中的错误:241:#如果已定义"使用"杂项| |定义"使用"XOPEN 242:/"用于'lgama'的过时别名。*/243:"MATHCALL(gamma,,("Mdouble));244:#endif 245:你介意只给我们看相关的代码吗?我个人总是在全局变量前面加上g
以避免这样的错误。此外,它还避免了局部变量命名混乱。我个人总是在全局变量前面加上g
以避免这样的错误。另外,它还避免了局部变量命名混乱锡安。