C++编译器错误:传递指针,但编译器看到引用

C++ compiler error: passing pointers but compiler sees references

本文关键字:编译器 引用 错误 C++ 指针      更新时间:2023-10-16

下面是类中的方法签名。

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是类名。我正在使用双指针(两个星号)。如何解决此错误?

在第二个签名中,第二个形式参数 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 **' 的参数会丢弃嵌套指针类型中的限定符。