Advertisement

c++ problem

Started by March 08, 2001 04:43 AM
9 comments, last by v71 23 years, 11 months ago
May i know why this is not working ? class aclass{ void (*FnPtr)(void) ; void Function( void ); public: . . . . . } if i write inside a body of a class function FnPtr=Function; the compiler gives me an error , why ? am i doing something wrong if i put the functio and the pointer outside the class works....
Try referencing the class function:

FnPtr = this->Function(); // from within

or

aclass MyClass;

MyClass.FnPtr = MyClass.Function();

Jim Adams
Advertisement
don''t know if this might be it, but you need a semicolon at the end of the block. example:

class blabla
{
public:
.
.
.
};
John Licatogamecreationlab.com
Also, don''t forget the '';'' at the end of your class

------------------------
CRAZY_DUSIK* pCrazyDuSiK;
pCrazyDuSiK->EatMicroshaft(MS_MUNCH_BILL_GATES | MS_CHEW_BILL_GATES);
------------------------CRAZY_DUSIK* pCrazyDuSiK = new CRAZY_DUSIK;pCrazyDuSiK->EatMicroshaft(MS_MUNCH_BILL_GATES | MS_CHEW_BILL_GATES);pCrazyDuSiK->WebSiteURL = "http://www.geocities.com/dusik2000";
tried everything doesn''t work
this is the error i get

error C2440: ''='' : cannot convert from ''void (__thiscall _3DSFileParser::*)(void)'' to ''void (__cdecl *)(void)''
There is no context in which this conversion is possible

this happen only if the function and the pointer are inside
the class , outside everything works fine how could that be ?
some c++ guru out there ???

quote:
Original post by Jim Adams

Try referencing the class function:

FnPtr = this->Function(); // from within

or

aclass MyClass;

MyClass.FnPtr = MyClass.Function();

Jim Adams



Inside class members, all member functions and variables implicitly have a "this->". It''s never necessary to explicitly use "this" to access data or members of a class from within that class.

The last suggestion won''t work unless he makes the data public; both the function and pointer are private in his implementation.

It looks like you''re doing the right thing with the function pointer. The error is probably elsewhere (like the other posters suggested, maybe a semi-colon missing).


Advertisement
Sorry, I was off on the private/public declaration when omitted. I already know about the ''this'', but if you you dealing with static functions in classes, you must use it to reference the instance at times, as the static function doesn''t know which instance called it.

But, the errors he is getting is consistent as C++ automatically inserts this as the first function argument, unless you specify the function as static:


class aClass
{
void (*FnPtr)(void);
static void Function(void);

public:
aClass() { FnPtr = Function(); }
void Doit(void) { FnPtr(); }
}

void aClass::Function(void)
{
cout << "Test";
}

main()
{
aClass MyClass;

MyClass.Doit();
}



This is off the top of my head, but I think that should work.

Jim Adams

Read your error message, the fix is in there

error C2440: '=' : cannot convert from 'void (__thiscall _3DSFileParser::*)(void)' to 'void (__cdecl *)(void)'
There is no context in which this conversion is possible

The only difference is that one is called with __cdecl and one is __thiscall _3DSFileParser::*, you should be able to infer that your member variable is declared wrong, it should be:

void (3DSFileParser::*FnPtr)(void);

or, in the faked example

void (aclass::*FnPtr)(void);



Edited by - Don Neufeld on March 8, 2001 7:09:06 PM
I believe that if you need to assign the address of a static member function to a function pointer, you have to be fairly explicit in everything.

pfn = &(X::Function)
Yesterday is the past, tomorrow is the future. Today is a gift, that is why we call it the present.
Hi V7,

Your statement

FnPtr=Function;

is illegal in C++.

This is because, FnPtr is a pointer of a global function. In C++, pointer to a function is different from pointer to a member function because a member function implicitely has a "this" pointer associated with it where as a global function does not have one.

Possible solutions for the above problem are:

Solution 1.
Make the function "void Function (void)" as a static function or a global function.
i.e

class aclass{

void (*FnPtr)(void) ;

static void Function( void );

public:
.
.
.
.
.
}

FnPtr=Function; // legal

Solution 2:

Declare FnPtr as a pointer to a member function instead of just pointer to a funtion.

i.e
class aclass{

void (aclass::*FnPtr)(void) ;

void Function( void );

public:
.
.
.
.
.
}

FnPtr=Function; // legal


Hope this work for you.

Thanks
Arun




This topic is closed to new replies.

Advertisement