function pointer (how to pass as parameter)?
Hello,
I tried already different variations of statements for the function parameter, but I still do not have the right version of it. I have got a function pointer and want to pass it as function parameter. Please tell me what is the right usage of it?
Code:
{
public:
SpectrogramData(double (*b)(double, double), double rang1[2], double rang2[2]):
QwtRasterData(QwtDoubleRect
(rang1
[0], rang2
[0], rang1
[1]- rang1
[0], rang2
[1]- rang2
[0])) {
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]); //<<< << 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];
};
error:
Quote:
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*)
plot.cpp:97: note: SpectrogramData::SpectrogramData(const SpectrogramData&)
make[1]: *** [../obj/linux-amd64-gcc-lsb31/plot.o] Error 1
make: *** [obj_qt] Error 2
Re: function pointer (how to pass as parameter)?
I don't know if what you're doing is correct but at first you should typedef your types otherwise you get crazy.
For ex:
Code:
typedef double (*benchFunc)(double, double);
now you have defined that the type benchFunc is a pointer to a function that returns double and gets 2 doubles.
then:
Code:
benchFunc pointerName;
pointerName is the pointer to the function.
btw: this is C, not C++. With C++ you have to use pointers to members and it's a little bit more tricky ;)
For ex when you assign a pointer to a member, you have to specify the class name and use the operator &:
Code:
return new SpectrogramData( &SpectrogramData::pointerName, &rangeX1[2], &rangeX2[2]);
edit: no sorry, my misstake.
When you want to assign a function address to a pointer to member you have to use what I wrote before, so it would be:
Code:
pointerName = &SpectrogramData::functionName;
Re: function pointer (how to pass as parameter)?
If I try out your code, I get the following error:
Quote:
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 (* SpectrogramData::*)(double, double), const double*, const double*)'
plot.cpp:99: note: candidates are: SpectrogramData::SpectrogramData(double (*)(double, double), double*, double*)
plot.cpp:97: note: SpectrogramData::SpectrogramData(const SpectrogramData&)
make[1]: *** [../obj/linux-amd64-gcc-lsb31/plot.o] Error 1
make: *** [obj_qt] Error 2
Where do I have to define the function pointer with:
Code:
typedef double (*benchFunc)(double, double);
and where
Code:
benchFunc pointerName;
?
Re: function pointer (how to pass as parameter)?
Define it in the header file, before the class definition and this:
Code:
benchFunc pointerName;
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).
Re: function pointer (how to pass as parameter)?
I've done a little example in a file called test.cpp, with your code, compiled with
Code:
g++ test.cpp -o test
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 ;)
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;
}
Re: function pointer (how to pass as parameter)?
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:
Code:
SpectrogramData(double (SpectrogramData::*b)(double, double), double rang1[2], double rang2[2]);
but I want to call it with:
Code:
SpectrogramData(double (*b)(double, double), double rang1[2], double rang2[2]);
Re: function pointer (how to pass as parameter)?
Quote:
Originally Posted by
trallallero
I've done a little example in a file called test.cpp, with your code, compiled with
Code:
g++ test.cpp -o test
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 ;)
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;
}
Thank you for your help.
What is about the "copy" function in this class? Because it is the only place where I have got an error :)
Re: function pointer (how to pass as parameter)?
Ok, I understand that it's the first time you use pointers to functions :D
The only difference between this (from your code, a class member)
Code:
double (*benchFunc)(double, double); // function pointer
and my
Code:
typedef double (*benchFunc)(double, double); // definition of function pointer
benchFunc m_pFunc; // function pointer
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.
As you can see in my example, in the main I call your SpectrogramData in this way:
Code:
SpectrogramData sd(&testFunc, rang1, rang2);
where testFunc is the function passed to the SpectrogramData.
Re: function pointer (how to pass as parameter)?
Quote:
Originally Posted by
rambo83
Thank you for your help.
What is about the "copy" function in this class? Because it is the only place where I have got an error :)
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 :eek:
Once I've done a state machine with a matrix of pointers to functions :crying:
Ciao :)
Re: function pointer (how to pass as parameter)?
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)
Quote:
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
I use this code now:
Code:
{
public:
SpectrogramData(double (*b)(double, double), double rang1[2], double rang2[2]):
QwtRasterData(QwtDoubleRect
(rang1
[0], rang2
[0], rang1
[1]- rang1
[0], rang2
[1]- rang2
[0])) {
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];
};
Re: function pointer (how to pass as parameter)?
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) ...
Code:
SpectrogramData(double (*b)(double, double), const double rang1[2], const double rang2[2]):
QwtRasterData(QwtDoubleRect
(rang1
[0], rang2
[0], rang1
[1]- rang1
[0], rang2
[1]- rang2
[0])) {
...
}
{
return new SpectrogramData( benchFunc , rangeX1, rangeX2);
}
...
Why not trying to understand the messages of the compiler yourself ?
Uwe
Re: function pointer (how to pass as parameter)?
Thank you for taking time to help me.
I have solved my problem by using another constructor type, namely SpectrogramData( SpectrogramData &);
Code:
{
public:
SpectrogramData(double (*b)(double, double), double rang1[2], double rang2[2]):
QwtRasterData(QwtDoubleRect
(rang1
[0], rang2
[0], rang1
[1]- rang1
[0], rang2
[1]- rang2
[0])) {
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;
};
Re: function pointer (how to pass as parameter)?
There is absolutely no reason for you to add a member variable just to hold the value of "this".
Code:
{
return new SpectrogramData( *this ); //<< this is what you could do instead
}
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.
Re: function pointer (how to pass as parameter)?
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
Re: function pointer (how to pass as parameter)?
Quote:
Originally Posted by
rambo83
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
Reading a C++ book is time intensive? How much time did you spend trying to solve this problem and posting questions to the newsgroup before you finally got it to work?
Well, good luck with your project.
Best regards,
David