Pass a member function to a class to that affects the object itself

Hello guys, :)

I’m trying to load a function pointer to a class I created. But I’m getting a “non-matching function or call” when compiling. (I want to use the function to edit a member variable of an existing object).

my classes constructor looks like this:

1
2
3
LinkedSliderDoubleSpinBox::LinkedSliderDoubleSpinBox(double minValue, double maxValue, long int bins, void (*setValueFunction)(double), double (*getValueFunction)(), QWidget *parent) :
    QWidget(parent)
{...}


my call looks like this:

LinkedSliderDoubleSpinBox* constantFieldDirChanger = new LinkedSliderDoubleSpinBox(minConstantDir,maxConstantDir,sliderLength,((MainWindow*)parentWidget())->openGLApp->blochsim->setConstantFieldDirection,((MainWindow*)parentWidget())->openGLApp->blochsim->getConstantFieldDirection,this);
min and maxConstantDir are doubles, sliderLength is an integer

and the functions setConstantFieldDirection and getConstantFieldDirection look have the following prototypes:

1
2
void setConstantFieldDirection(double value);
double getConstantFieldDirection();
The class I created in order to link a QSlider to a QDoubleSpinBox.

I’m not so familiar with function pointers. So I don’t really know whether I did a good mistake with them. Could you guys please help? why doesn’t the call match the prototype?

Please do not misunderstand what I'm trying to do. I want to call the function in its object stack. The functions are set and get functions!

Thank you. Any efforts are highly apprecaited! :-)


Come on guys :(

is it impossible to do it? :(


Heeeeeeeeeeeeeeelp!!!
First
LinkedSliderDoubleSpinBox* constantFieldDirChanger = new LinkedSliderDoubleSpinBox is the dynamic allocation really necessary? (this ain't java)
(MainWindow*)parentWidget() wonder if that cast is necessary or safe
((MainWindow*)parentWidget())->openGLApp->blochsim->setConstantFieldDirection such a long chain could be a design flaw

But I’m getting a “non-matching function or call” when compiling.
http://www.cplusplus.com/forum/articles/40071/#msg216270


1
2
3
4
5
6
7
class foo{
  void bar(); //this is a method
  void (*Bar) (); //this is a function pointer
//if you want to be more clear
  typedef void (*void_funct)();
  void_funct foobar;
};

Please do not misunderstand what I'm trying to do. I want to call the function in its object stack. The functions are set and get functions!
I really don't understand. You pass those function, what is their purpose? They could not access anything inside the class so what you will be setting/getting ?
Last edited on
First
LinkedSliderDoubleSpinBox* constantFieldDirChanger = new LinkedSliderDoubleSpinBox is the dynamic allocation really necessary? (this ain't java)


I'm using GUI interfaces of Qt. The allocation isn't done like that directly, but rather the pointer is stored on the stack of the class, and the contents are stored on the heap. This is to have better control of this under the GUI if things have to get replaced or removed or so on.


(MainWindow*)parentWidget() wonder if that cast is necessary or safe
((MainWindow*)parentWidget())->openGLApp->blochsim->setConstantFieldDirection such a long chain could be a design flaw


(MainWindow*)parentWidget() is there because I want to access the MainWindow which is a parent of the current Dialog I'm doing this on. So the current dialog is defined as a class in MainWindow, and the "this" of MainWindow is passed upon the construction of the current dialog's object.

Please do not misunderstand what I'm trying to do. I want to call the function in its object stack. The functions are set and get functions!
I really don't understand. You pass those function, what is their purpose? They could not access anything inside the class so what you will be setting/getting ?


I'm using a slider and a spinbox together to change a variable inside the object blochsim. Since this is a GUI program, there is a nasty hierarchy I have to follow (that nasty chain) in order to access the object which has the variable to be changed.

The class contains the slider and the spinbox linked together, and the passed functions are the functions that change the variables when a change in the state of the slider or the spinbox occures.

Sorry if it wasn't clear, if it still not, please ask for more explanation. I can't be very objective when I'm thinking so hard about a problem :-)
The class contains the slider and the spinbox linked together, and the passed functions are the functions that change the variables when a change in the state of the slider or the spinbox occures.
which variables? global?
How will you use those functions? (I'm starting to think that they are methods, so you need a object to operate them)

Please post the compiler errors (and be patient)
Last edited on
Thank you for your fast answer.

No they're not global, they're located in the object blochsim, which can be accessed through the hierarchy mentioned in the first post.

Yes they are methods (if you call member functions of a class methods, I'm not so familiar with the different names), and the object that operates them is blochsim, which is an object instance of the class Blochsim.

The error is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
..\optionswidget.cpp: In member function 'void OptionsWidget::createControls()':

..\optionswidget.cpp:161: error: no matching function for call to 'LinkedSliderD
oubleSpinBox::LinkedSliderDoubleSpinBox(qreal&, qreal&, int&, <unresolved overlo
aded function type>, <unresolved overloaded function type>, OptionsWidget* const
)'
..\/linkedsliderdoublespinbox.h:34: note: candidates are: LinkedSliderDoubleSpin
Box::LinkedSliderDoubleSpinBox(double, double, int, double&, QWidget*)
..\/linkedsliderdoublespinbox.h:33: note:                 LinkedSliderDoubleSpin
Box::LinkedSliderDoubleSpinBox(double, double, int, void (*)(double), double (*)
(), QWidget*)
..\/linkedsliderdoublespinbox.h:13: note:                 LinkedSliderDoubleSpin
Box::LinkedSliderDoubleSpinBox(const LinkedSliderDoubleSpinBox&)
mingw32-make[1]: *** [debug/optionswidget.o] Error 1
mingw32-make[1]: Leaving directory `X:/common_src/Blochsim_openGL/BlochSim/windo
wsBuild'
mingw32-make: *** [debug] Error 2 


Thank you!
if you call member functions of a class methods
Yes. The important is that a function pointer is not the same as a member function pointer (even if the parameters are the same type)
Please read these faqs http://www.parashift.com/c++-faq-lite/pointers-to-members.html

So the prototype of the function could be something like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
typedef void (Blochsim::*bloch_set) (double);  
typedef double (Blochsim::*bloch_get) ();

LinkedSliderDoubleSpinBox( 
  double minValue, double maxValue, long int bins, 
  bloch_set setValue, bloch_get getValue, 
  QWidget *parent
);

//And the call 
new LinkedSliderDoubleSpinBox(
  minConstantDir,maxConstantDir,sliderLength,
  &Blochsim::setConstantFieldDirection,  //I'm assuming that it is not a nested class
  &Blochsim::getConstantFieldDirection,
  this
);


However in order to apply a method you do need an object.
By instance
1
2
3
4
5
6
class LinkedSliderDoubleSpinBox{
  Blochsim blochsim;
};

//then apply the methods
(blochsim.*setValue)(42);

Now I wonder, why don't just use blochsim.setConstantFieldDirection(42); instead.

I hope it helps.
Last edited on
I went around the problem and solved it another way. I, unfortunately, avoided function pointers.

The last statement doesn't work because the blochsim is not a member in the stack I'm calling the function from. And I wanted to pass the function as a pointer.

Thank you for your help anyway :)
Topic archived. No new replies allowed.