0

My question is: is there a difference between the types/reference/pointer symbols for calling a function and the arguments in the function definition?

This question came up due to the following error I had:

myClass.cpp:358:44: error: no matching function for call to 'myClass::myFunction(double&, double&, double (*)[24], double (*)[30], int&)'
myClass.cpp:358:44: note: candidate is:
myClass.h:29:8: note: void myClass::myFunction(double&, double&, double**, double**, int&)

It is very weird that the compiler suggests that I should use double& for the first argument, because in fact a give a variable that is just double. Same for the second argument. But ok I can accept that... Why is the third and fourth argument not matching?

double a,b,c[24],d[30];
myFunction(a, b, &c, &d,e);

e is a int attribute of myClass. The function is

void myClass::myFunction(double& a,  double& b, double *c[24], double *d[30], int& e)
Ruben
  • 304
  • 3
  • 15

2 Answers2

1

Because myFunction expects double *[24], which is an array with 24 elements of type double*, not double (*)[24], which is a pointer to array double[24], they're not the same.

Try

double a, b, *c[24], *d[30];
myFunction(a, b, c, d, e);

LIVE

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
1

The compiler is telling you that the argument types in the function declaration do not match with the way the function is getting called.

When you have such an error, you'll need to go through each argument type and figure out which one(s) are responsible for the error.

In your case, the third and fourth arguments are problematic.

The argument type, double *c[24] means an array of 24 pointers to double.

The argument being used is &c, which, given your declaration of

double c[24];

means a pointer to an array of 24 doubles.

Methods to fix the problem:

Method 1: Change the declaration to use a pointer to an array

void myClass::myFunction(double& a,  double& b, double (*c)[24], double (*d)[30], int& e)

Then, the call can remain the same.

Method 2: Change the declaration to use a pointer

void myClass::myFunction(double& a,  double& b, double *c, double *d, int& e)

and then change the call to:

myFunction(a, b, c, d,e);
//               ^^ No & operator
// An array decays to a pointer to the first element

More on pointers to arrays and arrays of pointers: C pointer to array/array of pointers disambiguation

Community
  • 1
  • 1
R Sahu
  • 204,454
  • 14
  • 159
  • 270