samedi 2 septembre 2017

overloaded function with no contextual type information

#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");

Aucun commentaire:

Enregistrer un commentaire