Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

error C2440: '=' : cannot convert...

Status
Not open for further replies.

edgarasm

Programmer
Oct 29, 2002
26
BR
typedef int(*node_callback_type)(int);

I am using "node_callback_type" above as a function's pointer in order to determine which function I will use within the other function shortcoming below, but when I am using classes and method I was not able to use this resource because the following error "C2440: '=' : cannot convert from 'int (__thiscall shortcoming::*)(int)' to 'int (__cdecl *)(int)'" happens. Nevertheless If I do not use classes and method, then this resource "node_callback_type" works fine!


this peace of code maybe could help you understand that I am trying to explain:

void shortcoming::search(int col, int lin)
{
typedef int(*node_callback_type)(int);
node_callback_type process;

switch(Algoritm)
{
case 0: {
process=new_no_p1; (this is a method)
break;
}
case 1: {
process=new_no_p2; (this is a method)
break;
}
}

...

When I compile this code I get this error below:

error C2440: '=' : cannot convert from 'int (__thiscall shortcoming::*)(int)' to 'int (__cdecl *)(int)'

thanks in advance for any possible help I might receive!






 
It's the same problem as when calling windows API functions that require a callback as on of their parameters (I think). The problem is that methods in a class are prefixed with the "this" pointer (don't know exactly how to explain, but it's not just a function address, it's prefixed, since other instaces of this class have the same function, located elswhere).

What you might be able to do though, when working with classes, is to encapsulate the call to the method in two seperate classes using polymorphism. This is actually the object orientated way of using function pointers (which should not be necessary anymore in OOP).
Greetings,
Rick
 
you need to use it as a Method Pointer and not a Function Pointer....

typedef int(shortcoming::*node_callback_type)(int);

and when you call it you have to call it as

this->*process(int_value);

Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top