Re: [Dev-C++] typedef of pointers to functions compile error
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
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;
> }
>
> ------------------------------------------------------------------------------
|