6
#include<iostream>
#include<stdio.h>
#include<string.h>

using namespace std; 

void cb(int x)
{
    std::cout <<"print inside integer callback : " << x << "\n" ;    
}

void cb(float x)
{
    std::cout <<"print inside float callback :" << x <<  "\n" ;
}

void cb(std::string& x)
{
    std::cout <<"print inside string callback : " << x << "\n" ;
}

int main()
{

void(*CallbackInt)(void*);
void(*CallbackFloat)(void*);
void(*CallbackString)(void*);

CallbackInt=(void *)cb;
CallbackInt(5);

CallbackFloat=(void *)cb;
CallbackFloat(6.3);

CallbackString=(void *)cb;
CallbackString("John");

return 0;

}

Above is my code which has three function , I want to create three callbacks which will call overloaded function depending on their parameter. CallbackInt used to call cb function with int as parameter and similarly rest two.

But when compiling with it gives me error as below.

 function_ptr.cpp: In function ‘int main()’:
 function_ptr.cpp:29:21: error: overloaded function with no contextual type information
 CallbackInt=(void *)cb;
                 ^~
 function_ptr.cpp:30:14: error: invalid conversion from ‘int’ to ‘void*’ [-fpermissive]
 CallbackInt(5);
          ^
 function_ptr.cpp:32:23: error: overloaded function with no contextual type information
 CallbackFloat=(void *)cb;
                   ^~
 function_ptr.cpp:33:18: error: cannot convert ‘double’ to ‘void*’ in argument passing
 CallbackFloat(6.3);
              ^
 function_ptr.cpp:35:24: error: overloaded function with no contextual type information
 CallbackString=(void *)cb;
                    ^~
 function_ptr.cpp:36:24: error: invalid conversion from ‘const void*’ to ‘void*’ [-fpermissive]
 CallbackString("John");
Invictus
  • 4,028
  • 10
  • 50
  • 80
  • 3
    Why do have function pointers paramater `void*` ? --> `void(*CallbackInt)(int);` – kocica Sep 02 '17 at 18:28
  • 2
    And why all these casts? -> `CallbackInt=&cb;` (After changes by @FilipKočica) – Artemy Vysotsky Sep 02 '17 at 18:30
  • 1
    It would be better to write a calling wrapper with overloaded `operator ()`. If you need to store callbacks in separate variables then you need to correctly declare function pointer types instead of `void (void*);` – user7860670 Sep 02 '17 at 18:31
  • 3
    You have six occurrences of `void*` in the program, which is six occurrences too many. – n. m. could be an AI Sep 02 '17 at 18:37
  • Never write such bad code. Avoid C style cast in C++ program. Also, all of you callback are improperly defined. If they were properly defined, you would not need a cast. In fact, lot of cast is often an indication of poorly written code. **Read C++ books wrote by experts to correctly learn the langage.** – Phil1970 Sep 02 '17 at 19:35

3 Answers3

12

1) Compiler does not know, which overload of cb to chose.
2) Even if it did know, it would not convert from void* to, say void(*)(int), without any explicit conversion.

However, it can deduce it if you give compiler enough information:

void cb(int x)
{
    std::cout <<"print inside integer callback : " << x << "\n" ;
}

void cb(float x)
{
    std::cout <<"print inside float callback :" << x <<  "\n" ;
}

void cb(const std::string& x)
{
    std::cout <<"print inside string callback : " << x << "\n" ;
}

int main()
{

    void(*CallbackInt)(int);
    void(*CallbackFloat)(float);
    void(*CallbackString)(const std::string&);

    CallbackInt = cb;
    CallbackInt(5);

    CallbackFloat = cb;
    CallbackFloat(6.3);

    CallbackString = cb;
    CallbackString("John");

    return 0;

}
WindyFields
  • 2,697
  • 1
  • 18
  • 21
3

I know this is an old post, but I recently got this error and my issue was that I wasn't declaring the variable I was setting the result of my function to. Not sure why this error was the one it gave for that issue, but hopefully this helps someone.

Ethan Shoe
  • 466
  • 1
  • 7
  • 20
  • I had the same error due to the same issue (didn't declare variable which was supposed to receive the result of my function). My function was overloaded. I had the same problem elsewhere in my program, but with a non overloaded function and received a much less obtuse error message. fyi – Ken_sf Jul 16 '22 at 15:58
1

For the first error, use static cast to specify the overload you want, like here

For the rest, you can't cast an int nor a float to void* meaningfully. Not sure what you want to do here.

CallbackInt should probably take an int parameter...

Khoyo
  • 1,253
  • 11
  • 20