Thread: [Dev-C++] typedef of pointers to functions compile error
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Ben M. \(finlaylabs\) <bm...@fi...> - 2003-11-06 17:38:43
|
I have a prob with code that contains pointers to functions, that will =
not compile with Dev-C++. Other (knowlegeable)friends can make small =
mods and get it to compile on windows with VC++6.0, and on BSD systems =
etc, But Dev-C++ seems to choke on this one issue. I have used Dev-C++ =
for 6 months now and have been very impressed.
I am taking the liberty of pasting a post to Devshed below, that =
explains the issue. One friend (mitakeet) states that "Likely DevC wants =
some other way to declare the pointer. My research yesterday indicated =
that many compilers do not follow a standard, I just tried things until =
they worked."
He declared the pointer using this style:
#ifdef WIN32
#define FNCTPTR=20
#else //*nix
#define FNCTPTR &Data::
#endif
class Data{
private:
typedef int (Data::*FUNC_GET)(void);
and then
int GetData(FUNC_GET fn){
return (this->*fn)();
};
but that does not work on Dev-C++. Neither does my method, basically =
straight from the books by Stroustrup. The orig question post is below, =
with nice red highlighting to ease your viewing. How should I be =
declaring the pointer to function for Dev-C++?? Thanks for the help and =
the EXCELLENT software,
Ben Mclean
-------------------------------------------------------------------------=
-------
A friend and I are stumped on why the following wont compile. We get =
many error messages all as follows, for any function that contains calls =
to ddxFunc():=20
Calculations.cpp: In member function `double =
calculations::ddt3_Stress_xx(int, int, int)':=20
Calculations.cpp:176: no matching function for call to =
`Calculations::ddxFunc(<unknown type>, int&, int&, int&)'=20
Calculations.h:61: candidates are: double Calculations::ddxFunc(double =
(Calculations::*)(int, int, int), int, int, int)=20
The function ddt3_Stress_xx is below, the only point being it makes use =
of ddxFunc:=20
code:--------------------------------------------------------------------=
----------double Calculations::ddt3_Stress_xx(int i,int j,int k){
double ddt3stress;
ddt3stress =3D calcStiffness(11,i,j,k)*ddxFunc(ddt2_Vel_x,i,j,k)
+ calcStiffness(12,i,j,k)*ddyFunc(ddt2_Vel_y,i,j,k)
+ calcStiffness(13,i,j,k)*ddzFunc(ddt2_Vel_z,i,j,k);
if(myParams->symmetry>3){
ddt3stress +=3D =
calcStiffness(14,i,j,k)*(ddzFunc(ddt2_Vel_y,i,j,k)
+ ddyFunc(ddt2_Vel_z,i,j,k))
+ calcStiffness(15,i,j,k)*(ddzFunc(ddt2_Vel_x,i,j,k)
+ ddxFunc(ddt2_Vel_z,i,j,k))
+ calcStiffness(16,i,j,k)*(ddyFunc(ddt2_Vel_x,i,j,k)
+ ddxFunc(ddt2_Vel_y,i,j,k));
}
return ddt3stress;
}------------------------------------------------------------------------=
------
As far as we can determine we have correctly typedef'd etc etc such that =
it should be clear the FUNC argument is of type double. An extract of =
Calculations.h and .cpp showing how we have done this is below. What are =
we doing wrong, or what are we not doing that we need to be? I would =
greatly apprecate some help on this.=20
Thanks in anticipation,=20
finlaylabs.=20
code:--------------------------------------------------------------------=
----------//*************************************************************=
***************
//Calculations.h
class Calculations{
// typedef for passing functions as args
typedef double (Calculations::*FUNC)(int,int,int);
// should be clear that FUNC arguments return type double
public:
private:
double ddxFunc(FUNC,int,int,int);
double ddyFunc(FUNC,int,int,int);
double ddzFunc(FUNC,int,int,int);
//etc............
//***********************************************************************=
*****
//Calculations.cpp
#include <math.h>
#include "Calculations.h"
// derivatives of function-based values:
//dd*Func() can calc derivatives for ddt2_vel_x() etc function-derived =
values
double Calculations::ddxFunc(FUNC fn,int i,int j,int k){
double xderv =3D 0.0;
for(int K_index=3D1;K_index<=3DmyParams->K;K_index++){
xderv +=3D weightcoeff(K_index,myParams->dx)
*tapercoeff(K_index,myParams->dx)
=
*((this->*fn)(i+K_index,j,k)-(this->*fn)(i+1-K_index,j,k));
}
return xderv;
}
-------------------------------------------------------------------------=
----- |
|
From: Per W. <pw...@ia...> - 2003-11-06 22:06:07
|
All standard-compliant compilers should handle function pointers with
aplomb. Just do:
class Data {
public:
typedef int (*FUNC_GET)();
int GetData(FUNC_GET fn) {
return fn(); // Alternative 1
// return (*fn)(); // Alternative 2
}
};
int function() {
return 5;
}
int main() {
Data data;
return data.GetData(function);
}
/Per W
On Fri, 7 Nov 2003, Ben McLean (finlaylabs) wrote:
> I have a prob with code that contains pointers to functions, that will not compile with Dev-C++. Other (knowlegeable)friends can make small mods and get it to compile on windows with VC++6.0, and on BSD systems etc, But Dev-C++ seems to choke on this one issue. I have used Dev-C++ for 6 months now and have been very impressed.
> I am taking the liberty of pasting a post to Devshed below, that explains the issue. One friend (mitakeet) states that "Likely DevC wants some other way to declare the pointer. My research yesterday indicated that many compilers do not follow a standard, I just tried things until they worked."
>
> He declared the pointer using this style:
> #ifdef WIN32
> #define FNCTPTR
> #else //*nix
> #define FNCTPTR &Data::
> #endif
>
> class Data{
> private:
>
> typedef int (Data::*FUNC_GET)(void);
> and then
> int GetData(FUNC_GET fn){
> return (this->*fn)();
> };
> but that does not work on Dev-C++. Neither does my method, basically straight from the books by Stroustrup. The orig question post is below, with nice red highlighting to ease your viewing. How should I be declaring the pointer to function for Dev-C++?? Thanks for the help and the EXCELLENT software,
>
> Ben Mclean
>
>
> --------------------------------------------------------------------------------
> A friend and I are stumped on why the following wont compile. We get many error messages all as follows, for any function that contains calls to ddxFunc():
>
> Calculations.cpp: In member function `double calculations::ddt3_Stress_xx(int, int, int)':
> Calculations.cpp:176: no matching function for call to `Calculations::ddxFunc(<unknown type>, int&, int&, int&)'
> Calculations.h:61: candidates are: double Calculations::ddxFunc(double (Calculations::*)(int, int, int), int, int, int)
>
> The function ddt3_Stress_xx is below, the only point being it makes use of ddxFunc:
>
> code:------------------------------------------------------------------------------double Calculations::ddt3_Stress_xx(int i,int j,int k){
> double ddt3stress;
> ddt3stress = calcStiffness(11,i,j,k)*ddxFunc(ddt2_Vel_x,i,j,k)
> + calcStiffness(12,i,j,k)*ddyFunc(ddt2_Vel_y,i,j,k)
> + calcStiffness(13,i,j,k)*ddzFunc(ddt2_Vel_z,i,j,k);
> if(myParams->symmetry>3){
> ddt3stress += calcStiffness(14,i,j,k)*(ddzFunc(ddt2_Vel_y,i,j,k)
> + ddyFunc(ddt2_Vel_z,i,j,k))
> + calcStiffness(15,i,j,k)*(ddzFunc(ddt2_Vel_x,i,j,k)
> + ddxFunc(ddt2_Vel_z,i,j,k))
> + calcStiffness(16,i,j,k)*(ddyFunc(ddt2_Vel_x,i,j,k)
> + ddxFunc(ddt2_Vel_y,i,j,k));
> }
> return ddt3stress;
> }------------------------------------------------------------------------------
> As far as we can determine we have correctly typedef'd etc etc such that it should be clear the FUNC argument is of type double. An extract of Calculations.h and .cpp showing how we have done this is below. What are we doing wrong, or what are we not doing that we need to be? I would greatly apprecate some help on this.
>
> Thanks in anticipation,
> finlaylabs.
>
>
> code:------------------------------------------------------------------------------//****************************************************************************
> //Calculations.h
>
> class Calculations{
> // typedef for passing functions as args
> typedef double (Calculations::*FUNC)(int,int,int);
> // should be clear that FUNC arguments return type double
> public:
>
> private:
>
> double ddxFunc(FUNC,int,int,int);
> double ddyFunc(FUNC,int,int,int);
> double ddzFunc(FUNC,int,int,int);
> //etc............
>
>
> //****************************************************************************
> //Calculations.cpp
>
> #include <math.h>
>
> #include "Calculations.h"
>
> // derivatives of function-based values:
> //dd*Func() can calc derivatives for ddt2_vel_x() etc function-derived values
> double Calculations::ddxFunc(FUNC fn,int i,int j,int k){
> double xderv = 0.0;
> for(int K_index=1;K_index<=myParams->K;K_index++){
> xderv += weightcoeff(K_index,myParams->dx)
> *tapercoeff(K_index,myParams->dx)
> *((this->*fn)(i+K_index,j,k)-(this->*fn)(i+1-K_index,j,k));
> }
> return xderv;
> }
>
> ------------------------------------------------------------------------------
|
|
From: Stefan K. <ste...@ya...> - 2003-11-07 07:26:51
|
Hi Per,
I think Ben was referring to C++ function pointers, which are pointers
to class member functions, not global C/C++ functions.
Ben, the class Data code should really work in all standard compilers.
It the same as I did with MSVC 6.0, and really as in Stroustrup.
However, the trick is how to supply a method function pointer as an
argument to a function.
This is the point that is suprisingly different from how you would do it
in C, as you *HAVE* to use the address operator on the full-qualified
function name:
Data data;
int x = data.GetFunc( &Data::ddxfunc );
Furthermore, ddxfunc must be an existing member function of class Data,
of course.
Hope this helps!
Best,
Stefan
--
Stefan Kuhn
Software Development
ste...@ya...
Per Westermark wrote:
> All standard-compliant compilers should handle function pointers with
> aplomb. Just do:
>
> class Data {
> public:
> typedef int (*FUNC_GET)();
> int GetData(FUNC_GET fn) {
> return fn(); // Alternative 1
> // return (*fn)(); // Alternative 2
> }
> };
>
> int function() {
> return 5;
> }
>
> int main() {
> Data data;
> return data.GetData(function);
> }
>
> /Per W
>
>
> On Fri, 7 Nov 2003, Ben McLean (finlaylabs) wrote:
>
>
>>I have a prob with code that contains pointers to functions, that will not compile with Dev-C++. Other (knowlegeable)friends can make small mods and get it to compile on windows with VC++6.0, and on BSD systems etc, But Dev-C++ seems to choke on this one issue. I have used Dev-C++ for 6 months now and have been very impressed.
>>I am taking the liberty of pasting a post to Devshed below, that explains the issue. One friend (mitakeet) states that "Likely DevC wants some other way to declare the pointer. My research yesterday indicated that many compilers do not follow a standard, I just tried things until they worked."
>>
>>He declared the pointer using this style:
>>#ifdef WIN32
>> #define FNCTPTR
>>#else //*nix
>> #define FNCTPTR &Data::
>>#endif
>>
>>class Data{
>>private:
>>
>>typedef int (Data::*FUNC_GET)(void);
>>and then
>>int GetData(FUNC_GET fn){
>> return (this->*fn)();
>>};
>>but that does not work on Dev-C++. Neither does my method, basically straight from the books by Stroustrup. The orig question post is below, with nice red highlighting to ease your viewing. How should I be declaring the pointer to function for Dev-C++?? Thanks for the help and the EXCELLENT software,
>>
>>Ben Mclean
>>
>>
>>--------------------------------------------------------------------------------
>>A friend and I are stumped on why the following wont compile. We get many error messages all as follows, for any function that contains calls to ddxFunc():
>>
>>Calculations.cpp: In member function `double calculations::ddt3_Stress_xx(int, int, int)':
>>Calculations.cpp:176: no matching function for call to `Calculations::ddxFunc(<unknown type>, int&, int&, int&)'
>>Calculations.h:61: candidates are: double Calculations::ddxFunc(double (Calculations::*)(int, int, int), int, int, int)
>>
>>The function ddt3_Stress_xx is below, the only point being it makes use of ddxFunc:
>>
>>code:------------------------------------------------------------------------------double Calculations::ddt3_Stress_xx(int i,int j,int k){
>> double ddt3stress;
>> ddt3stress = calcStiffness(11,i,j,k)*ddxFunc(ddt2_Vel_x,i,j,k)
>> + calcStiffness(12,i,j,k)*ddyFunc(ddt2_Vel_y,i,j,k)
>> + calcStiffness(13,i,j,k)*ddzFunc(ddt2_Vel_z,i,j,k);
>> if(myParams->symmetry>3){
>> ddt3stress += calcStiffness(14,i,j,k)*(ddzFunc(ddt2_Vel_y,i,j,k)
>> + ddyFunc(ddt2_Vel_z,i,j,k))
>> + calcStiffness(15,i,j,k)*(ddzFunc(ddt2_Vel_x,i,j,k)
>> + ddxFunc(ddt2_Vel_z,i,j,k))
>> + calcStiffness(16,i,j,k)*(ddyFunc(ddt2_Vel_x,i,j,k)
>> + ddxFunc(ddt2_Vel_y,i,j,k));
>> }
>> return ddt3stress;
>>}------------------------------------------------------------------------------
>>As far as we can determine we have correctly typedef'd etc etc such that it should be clear the FUNC argument is of type double. An extract of Calculations.h and .cpp showing how we have done this is below. What are we doing wrong, or what are we not doing that we need to be? I would greatly apprecate some help on this.
>>
>>Thanks in anticipation,
>>finlaylabs.
>>
>>
>>code:------------------------------------------------------------------------------//****************************************************************************
>>//Calculations.h
>>
>>class Calculations{
>> // typedef for passing functions as args
>> typedef double (Calculations::*FUNC)(int,int,int);
>> // should be clear that FUNC arguments return type double
>>public:
>>
>>private:
>>
>> double ddxFunc(FUNC,int,int,int);
>> double ddyFunc(FUNC,int,int,int);
>> double ddzFunc(FUNC,int,int,int);
>>//etc............
>>
>>
>>//****************************************************************************
>>//Calculations.cpp
>>
>>#include <math.h>
>>
>>#include "Calculations.h"
>>
>>// derivatives of function-based values:
>>//dd*Func() can calc derivatives for ddt2_vel_x() etc function-derived values
>>double Calculations::ddxFunc(FUNC fn,int i,int j,int k){
>> double xderv = 0.0;
>> for(int K_index=1;K_index<=myParams->K;K_index++){
>> xderv += weightcoeff(K_index,myParams->dx)
>> *tapercoeff(K_index,myParams->dx)
>> *((this->*fn)(i+K_index,j,k)-(this->*fn)(i+1-K_index,j,k));
>> }
>> return xderv;
>>}
>>
>>------------------------------------------------------------------------------
>
>
>
>
> -------------------------------------------------------
> This SF.net email is sponsored by: SF.net Giveback Program.
> Does SourceForge.net help you be more productive? Does it
> help you create better code? SHARE THE LOVE, and help us help
> YOU! Click Here: http://sourceforge.net/donate/
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|
|
From: Stefan K. <ste...@ya...> - 2003-11-07 07:49:20
|
Hi Ben,
I got this piece of code to compile under Dev-Cpp. It illustrates how to
use member function pointers in C++. From what I know, it is ISO
compliant to do it this way, so it should work with all ISO compliant
compilers.
Best,
Stefan
class Data{
public:
typedef int (Data::*FUNC_GET)(void);
int GetData(FUNC_GET fn){ return (this->*fn)(); }
int Test1(void) { return 5; }
};
Data data;
int x = data.GetData( &Data::Test1 );
--
Stefan Kuhn
Software Development
ste...@ya...
|