Santosh Tiwari
Santosh Tiwari

Reputation: 1216

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

The following is the method signature in a class.

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
}

Here is the declaration of arguments

unsigned int size;
double **var = new double*[size];
double **obj = new double*[size];
double **constr = new double*[size];

Here is the method call.

evaluator.evaluate(size, var, obj, constr);

I get the following compiler error.

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 are class names. I am using double pointers (two asterisks). How do I resolve this error?

Upvotes: 2

Views: 788

Answers (1)

ach
ach

Reputation: 2373

In your second signature, the type of the second formal parameter, var, is const double**. The actual argument, constr, is, hovewer of type double** which cannot be implicitly converted to the former type.

Example

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

The error reported is accurate:

main.c:17:8: Passing 'int **' to parameter of type 'const int **' discards qualifiers in nested pointer types.

Upvotes: 3

Related Questions