LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-21-2005, 10:56 AM   #1
allomeen
Member
 
Registered: Dec 2005
Posts: 83

Rep: Reputation: 15
C++ classes help


Hello everyone,
I have this problem and I don't know how to solve it. I have two classes Timer and Power. The Timer class have a member function that accept a void *(*ptr)() ;a pointer to a function and then start a thread and does some things. the Power class instantiate a Timer object and calls that function passing an address of one of its member function.

class Timer
{
public:
void setFunction(void *(*fun)());
}

class Power
{
public:
Timer t;
private:
Power();
void * update();
}

Power::Power()
{
//...code...//
t.setFunction(update);
//...code...//
}
Power::update()
{
//...code...//
}

The compiler gives me this error:
"error: no matching function for call to `Timer::setFunction(<unkown type>)'
note: candidates are: void Timer::setFunction(void*(*)())"

Can anybody tell me what wrong here and why doesn't the compiler know the type of update?

Thanks,
Alaa G
 
Old 12-21-2005, 11:11 AM   #2
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
To use a class function as a function ptr the function has to be static to the class, but this also means it can not call any members of the class which are not static. The way I normaly get around this is to use a friend function helper, something like this:
Code:
int helper(void* data)
{
   //start the thread
   ....
}

class Foo
{
  public:
   ...
  friend int helper(void* data);
  private:
  ....
};
just as a side note is this correct?
Code:
class Power
{
public:
Timer t;
private:
Power();
void * update();
};
or did you mean
Code:
class Power
{
private:
Timer t;
public:
Power();
void * update();
};

Last edited by dmail; 12-21-2005 at 11:18 AM.
 
Old 12-21-2005, 11:11 AM   #3
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
try declaring this function as
Code:
void setFunction(void *(*)() fun);
it may help to create a typedef for your function pointer type.
 
Old 12-21-2005, 11:19 AM   #4
allomeen
Member
 
Registered: Dec 2005
Posts: 83

Original Poster
Rep: Reputation: 15
Thanks guys. I just declared update to be static and it worded.
 
Old 12-26-2005, 04:47 PM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Does the 'update' function need to do anything to the 'Power' object? If it's static, it might as well be outside of the class. You can make it work the other way if you change 'setFunction' to
Code:
//Put this before Timer
class Power;

//Within the Timer class
void setFunction(Power *power, void *(Power:: *fun)());
This will allow you to tie the Timer directly to the Power object. Then change the 'setFunction' call to
Code:
t.setFunction(this, &Power::update);
When you call the function from Timer, call it like this
Code:
(power->*fun)();
That will make sure the Power object that called 'setFunction' will get the function call (i.e. you can update information within the Power object in the 'update' function.) If you leave it static, none of the Power objects will get the call; it will be like calling a global function.

You should always use '&' when giving a function pointer argument just out of good form, and you should also qualify even static functions with the class name, just in case.
ta0kira

PS dmail: OP is using a private function pointer, however OP provides it from within the class so there is no need for friends or making the function public. I do agree that 't' should be private as well, however.

Last edited by ta0kira; 12-26-2005 at 04:54 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Classes in VC++ Diederick Programming 2 11-30-2005 10:57 AM
Possible uses for classes. RHLinuxGUY Programming 4 11-21-2005 10:10 PM
classes in C++ niteshadw Programming 2 07-05-2005 07:05 PM
C++ nested classes enemorales Programming 5 05-23-2005 03:40 PM
OOP (PHP) classes and extended classes ldp Programming 3 03-05-2005 11:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:55 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration