Define it in the header file, before the class definition and this:
is a class member (I guess private).
It's better if you send me all the code you're trying to compile
(but soon I'll go home so I cannot help you before tomorrow).
Define it in the header file, before the class definition and this:
is a class member (I guess private).
It's better if you send me all the code you're trying to compile
(but soon I'll go home so I cannot help you before tomorrow).
I've done a little example in a file called test.cpp, with your code, compiled with
and works.
Just to show you how to use the pointers to functions.
But you should learn how to use this in C++ with pointers to methods
Qt Code:
#include <stdio.h> typedef double (*benchFunc)(double, double); // definition of function pointer class SpectrogramData { public: SpectrogramData(double (*b)(double, double), double rang1[2], double rang2[2]) { m_pFunc = b; rangeX1[0] = rang1[0]; rangeX1[1] = rang1[1]; rangeX2[0] = rang1[0]; rangeX2[1] = rang2[1]; } virtual double value(double x, double y) const { const double result = m_pFunc(x,y); return result; } private: benchFunc m_pFunc; // function pointer double rangeX1[2]; double rangeX2[2]; }; double testFunc(double d1, double d2) { return d1 + d2; } int main() { double rang1[2] = {1.1, 2.2}; double rang2[2] = {3.3, 4.4}; SpectrogramData sd(&testFunc, rang1, rang2); double res = sd.value(1.1, 2.2); printf("res: <%lf>\n", res); return 0; }To copy to clipboard, switch view to plain text mode
I had to remove it from the example as I don't have the class QwtRasterData.
But now I have to go, I hope you'll solve the problem otherwise I'll check it tomorrow.
Don't worry, every time you start with pointers to functions you get a bit crazy, it's normal
Once I've done a state machine with a matrix of pointers to functions
Ciao![]()
rambo83 (30th November 2009)
Unfortunately, I cannot remove the "copy" function, because the class QwtRasterData needs it. I have still the problem, whereby I think that the reason is the different types - the one is without "const" and another with (see error)
I use this code now:make
Compiling ./qt/plot.cpp
plot.cpp: In constructor 'SimpleData::SimpleData(double, double, double)':
plot.cpp:60: warning: passing 'double' for argument 1 to 'int abs(int)'
plot.cpp: In member function 'virtual QwtRasterData* SpectrogramData::copy() const':
plot.cpp:109: error: no matching function for call to 'SpectrogramData::SpectrogramData(double (* const&)(double, double), const double*, const double*)'
plot.cpp:99: note: candidates are: SpectrogramData::SpectrogramData(double (*)(double, double), double*, double*) <near match>
plot.cpp:97: note: SpectrogramData::SpectrogramData(const SpectrogramData&)
make[1]: *** [../obj/linux-amd64-gcc-lsb31/plot.o] Error 1
make: *** [obj_qt] Error 2
Qt Code:
{ public: SpectrogramData(double (*b)(double, double), double rang1[2], double rang2[2]): { benchFunc = b; rangeX1[0] = rang1[0]; rangeX1[1] = rang1[1]; rangeX2[0] = rang1[0]; rangeX2[1] = rang2[1]; } { return new SpectrogramData( benchFunc , &rangeX1[2], &rangeX2[2]); } { } virtual double value(double x, double y) const { const double result = benchFunc(x,y); return result; } private: double (*benchFunc)(double, double); // function pointer double rangeX1[2]; double rangeX2[2]; };To copy to clipboard, switch view to plain text mode
a) Your constructor needs const qualifiers, because you are passing members of the class in copy - what is a const method.
b) The implementation of copy will produces runtime crashes, because you are passing bad adresses for the constructor of the clone.
c) Use fabs instead of abs
d) ...
Qt Code:
SpectrogramData(double (*b)(double, double), const double rang1[2], const double rang2[2]): { ... } { return new SpectrogramData( benchFunc , rangeX1, rangeX2); } ...To copy to clipboard, switch view to plain text mode
Why not trying to understand the messages of the compiler yourself ?
Uwe
rambo83 (30th November 2009)
Thank you for taking time to help me.
I have solved my problem by using another constructor type, namely SpectrogramData( SpectrogramData &);
Qt Code:
{ public: SpectrogramData(double (*b)(double, double), double rang1[2], double rang2[2]): { benchFunc = b; rangeX1[0] = rang1[0]; rangeX1[1] = rang1[1]; rangeX2[0] = rang1[0]; rangeX2[1] = rang2[1]; itself = this; } { return new SpectrogramData( *itself); //<<< << this is the reason for the error } { } virtual double value(double x, double y) const { const double result = benchFunc(x,y); return result; } private: double (*benchFunc)(double, double); // function pointer double rangeX1[2]; double rangeX2[2]; SpectrogramData *itself; };To copy to clipboard, switch view to plain text mode
There is absolutely no reason for you to add a member variable just to hold the value of "this".
Qt Code:
{ return new SpectrogramData( *this ); //<< this is what you could do instead }To copy to clipboard, switch view to plain text mode
But what you really *should* do is to sit down with your C++ textbook or someone who is a C++ expert and ask them to explain pointers to functions, pointers to methods, typedefs, and the proper way to pass pointers in function calls so that you really understand why what you were originally trying to do wasn't working.
Trying things at random until you finally get it to compile doesn't teach you anything. And just because it compiles doesn't mean it won't blow up at run time. If you don't understand what you did to fix it, you certainly won't know why it blows up.
For one thing, this: "&rangeX1[2]" is passing the address of the *third* entry in the rangeX1 array. Since rangeX1 only has two elements, that's a problem. But it also explains the compiler error you were getting. Your constructor is expecting that the last two arguments are pointers to arrays of doubles of size two. You were passing pointers to const doubles (the addresses of third elements of your range array member variables).
The code Uwe gave you was correct, and uses your original constructor.
That you fixed the code to use a copy constructor is a lucky accident, in my opinion. Your code does not define a copy constructor, so the compiler is supplying a default one for you, which does a bit-wise copy of the source SpectrogramData instance into the new one. If this class happened to have any member variables that *couldn't* be copied in that way (like a pointer to a "newed" member variable), this would blow up at some point when the first instance went out of scope and that pointer got deleted.
rambo83 (1st December 2009)
Thank you for extensive explanation and advices, d_stranz.
Of course, I have to learn much about c++ programming yet. Now it is important for me to build up a GUI framework because it is my internship project. I don't have much time for implementation, so that I likely try random things out than reading a c++ book, which is more time intensive.
Thank you again for help.
best regards,
Vitali
I'm afraid that if I use the function pointer as a member variable, then it is not possible to pass a normal (not member variable) function pointer to the constructor of "SpectrogramData" class, because then the parameters of constructor and copy method will be of different types, right? Then I will have to call my constructor with:
but I want to call it with:Qt Code:
SpectrogramData(double (SpectrogramData::*b)(double, double), double rang1[2], double rang2[2]);To copy to clipboard, switch view to plain text mode
Qt Code:
SpectrogramData(double (*b)(double, double), double rang1[2], double rang2[2]);To copy to clipboard, switch view to plain text mode
Ok, I understand that it's the first time you use pointers to functions
The only difference between this (from your code, a class member)
Qt Code:
double (*benchFunc)(double, double); // function pointerTo copy to clipboard, switch view to plain text mode
and my
is that it's more readable as you don't have to write "double (*benchFunc)(double, double)" each time you want to refer to that type.Qt Code:
typedef double (*benchFunc)(double, double); // definition of function pointer benchFunc m_pFunc; // function pointerTo copy to clipboard, switch view to plain text mode
As you can see in my example, in the main I call your SpectrogramData in this way:
where testFunc is the function passed to the SpectrogramData.Qt Code:
SpectrogramData sd(&testFunc, rang1, rang2);To copy to clipboard, switch view to plain text mode
Bookmarks