【发布时间】:2014-06-17 22:39:53
【问题描述】:
我正在使用 linux 机器并使用 g++ 编译 .cpp 文件。我不断收到以下错误:
hillClimbing.cpp:6: error: expected unqualified-id before â[â token
hillClimbing.cpp:7: error: expected unqualified-id before â[â token
hillClimbing.cpp:8: error: expected unqualified-id before â[â token
hillClimbing.cpp:17: error: expected unqualified-id before â[â token
我一直在查看此站点上的类似帖子,但它们似乎与我的问题不符,我无法解决它。这是 .cpp 文件:
#include <utility>
#include "evaluateParams.h"
using namespace std;
double[] optimizeParams(EvaluateParams eval, pair<double, double> ranges[], double ss[], double a, double e, bool hillClimb, bool ascent);
double[] initParams(pair<double, double> ranges[]);
double[] climbHill(double pos[], double stepSize[], double epsilon, bool findMax);
evaluateParams evalMethod;
double accel;
//eval/evalMethod: defines method used to compare a set of params
//ranges: list of ranges that each parameter can take
//ppp: points per parameter, the number of points initialized is ppa^ranges.size()
//hillClimb: indicates weather to enact the hillClimb algorithm or simply randomize
double[] optimizeParams(evaluateParams eval, pair<double, double> ranges[], double ss[], double a, double e, bool hillClimb, bool ascent){
evalMethod = eval;
accel = a;
double paramValues[] = initParams(ranges);
if(hillClimb){
paramValues = climbHill(paramValues[i], ss, e, ascent);
}
return paramValues;
}
double[] initParams(pair<double, double> ranges[]){
double values[ranges.size()];
for(int j = 0; j < ranges.size(); j++){
double min = ranges[i].first;
double max = ranges[i].second;
double r = ((double) rand() / (RAND_MAX));
r *= max-min;
r += min;
values[i][j]=r;
}
return values;
}
double[] climbHill(double pos[], double stepSize[], double epsilon, bool findMax){
double candidate[] = [-accel, -1/accel, 0, 1/accel, accel];
while(true){ //may need to switch to a timer in case points get stuck
double init = eval.evaluate(pos);
for(int i = 0; i < pos.size(); i++){
int best = -1;
double bestEval = -1 * numeric_limits<double>::max();
for(int j = 0; j < candidate.size(); j++){
pos[i] = pos[i] + stepSize[i]*candidate[j];
double temp = eval.evaluate(tempPos);
pos[i] = pos[i] - stepSize[i]*candidate[j];
if(temp > bestScore){
bestEval = temp;
best = j;
}
}
if(findMax){
pos[i] = pos[i] + stepSize[i] * candidate[best];
}
else{
pos[i] = pos[i] - stepSize[i] * candidate[best];
}
if(candidate[best] != 0){
stepSize[i] = stepSize[i] * candidate[best];
}
}
if(eval.evaluate(pos)-init < epsilon){
return pos;
}
}
}
这里是 .h 文件:
class evaluateParams{
public:
evaluateParams(){}
virtual double evaluate(double[]);
};
我看到的大多数其他帖子都是通过添加“;”来修复的在头文件的末尾,我已经完成了。
【问题讨论】:
-
您不能返回 C 数组。您可能应该使用
std::vector。 -
double values[ranges.size()];是非法的;数组大小必须在编译时知道。 (一些编译器有一个扩展允许这样做,但在更复杂的情况下会导致有问题的行为)。 -
climbHill也有很多大问题,这看起来像是有人复制粘贴了 Java 代码并更改了一些东西
标签: c++ linux compilation g++