以下是类中的方法签名。
virtual void evaluate(const double *var, double *obj, double *constr) const = 0;
virtual void evaluate(unsigned int numPoints, const double **var, double **obj, double **constr) const {
//do something
}
这是参数声明
unsigned int size;
double **var = new double*[size];
double **obj = new double*[size];
double **constr = new double*[size];
这是方法调用。
evaluator.evaluate(size, var, obj, constr);
我收到以下编译错误。
foo.cpp: In member function âvoid foo::evaluatePopulation(std::vector<Individual, std::allocator<Individual> >&, unsigned int, bool)â:
foo.cpp:347: error: no matching function for call to foo::evaluate(unsigned int&, double**&, double**&, double**&) constâ
foo.h:35: note: candidates are: virtual void foo::evaluate(const double*, double*, double*) const
foo.h:43: note: virtual void foo::evaluate(unsigned int, const double**, double**, double**) const <near match>
foo
是类名。我正在使用双指针(两个星号)。如何解决此错误?
答案 0 :(得分:3)
在第二个签名中,第二个形式参数var
的类型为const double**
。实际参数constr
是类型double**
的hovewer,它不能隐式转换为前一种类型。
示例强>
#include <stdio.h>
void fn(const int** pp)
{
printf("%p : %p : %d", pp, *pp, **pp);
}
int main()
{
int n = 1;
int *p = &n;
fn(&p); // ERROR. see below
return 0;
}
报告的错误是准确的:
main.c:17:8:将
'int **'
传递给类型'const int **'
的参数会丢弃嵌套指针类型中的限定符。