简体   繁体   中英

C++ overloaded function calling version of itself with more arguments

I am using function overload to have a general version of a behaviour and a more usual one. The usual function just picks a default value for the second argument that actually depends on the first, and the compiler is giving me an error because it does not even recognize the existence of the second function. I also tried to do it with default values, but because the default depends on the first argument, the compiler does not seem to accept it.

So, here are simplified examples just for illustration. Function overloading case:

#include <stdio.h>  
struct pair {
  int x;
  int y;
};

int func(pair a){
  return func(a, a.y);
}

int func(pair a, int b) {
  return a.x*b;
}

int main() {
  pair z;
  z.x = 2;
  z.y = 4;
  printf("%d\n", func(z));
  printf("%d\n", func(z,12));
}

This gives me the error:

a.c: In function ‘int func(pair)’:
a.c:9:21: error: too many arguments to function ‘int func(pair)’
a.c:8:5: note: declared here"

Example with default values:

#include <stdio.h>

struct pair {
  int x;
  int y;
};


int func(pair a, int b = a.y) {
  return a.x*b;
}
int main() {
     pair z;
     z.x = 2;
     z.y = 4;
     printf("%d\n", func(z));
     printf("%d\n", func(z,12));
}

Gives me the following error: "local variable a may not appear in this context"

So, is there any way in C++ to emulate this behaviour? I never had this problem in other languages, like Java or even in ASP.

Thank you all.

In C and C++, before the function call, that function should be declared or defined. Here you are making a call to return func(a, ay); but the function func(pair, int) has not yet been declared or defined.

You need to change the definitions of the two functions, or just declare the functions in the beginning of your code. As other answers have explained the first approach, here is the snippet with second approach.

  #include <stdio.h> 
  //Function Declaration
  int func(pair);
  int func(pair, int); 
  struct pair {
      int x;
      int y;
  };

  int func(pair a){
       return func(a, a.y);
  }

  int func(pair a, int b) {
      return a.x*b;
  }

  int main() {
      pair z;
      z.x = 2;
      z.y = 4;
      printf("%d\n", func(z));
      printf("%d\n", func(z,12)); 
  }

Switch the order of the definitions of func(), such that the 2 argument version is defined before the one argument version. The compiler doesn't know the 2 argument version exists until it encounters the definition, so you can't call it until you've told the compiler it exists.

You have to change the order of the definitions:

int func(pair a, int b) {
  return a.x*b;
}

int func(pair a){
  return func(a, a.y);
}

LIVE DEMO

This is happening because in int func(pair a) you are calling int func(pair a, int b) which is not visible. Changing the order of definitions like above solves this problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM