cannot convert parameter from 'double (double)' to 'double (__cdecl *)(double)' error

Discussion in 'C++' started by Sydex, Feb 17, 2005.

  1. Sydex

    Sydex Guest

    When I compile code I get error
    C2664: 'Integration::qgaus' : cannot convert parameter 1 from 'double
    (double)' to 'double (__cdecl *)(double)'
    in this part :
    double Integration::quad2d(double (*func)(double,double))
    {

    nfunc = func ;

    return qgaus(f1,x1,x2);//error there

    }

    //double (*nfunc)(double,double);

    //double qgaus(double (*f)(double),double min,double max) const;

    Please help
     
    Sydex, Feb 17, 2005
    #1
    1. Advertising

  2. Sydex

    Sydex Guest

    I forgot
    // double Integration::f1(double x)
     
    Sydex, Feb 17, 2005
    #2
    1. Advertising

  3. Sydex

    SnaiL Guest

    What is f1? Your question is correct, but code you have provide is too
    bad to understand where is a problem.
     
    SnaiL, Feb 17, 2005
    #3
  4. Re: cannot convert parameter from 'double (double)' to 'double (__cdecl*)(double)' error

    Sydex wrote:
    > When I compile code I get error
    > C2664: 'Integration::qgaus' : cannot convert parameter 1 from 'double
    > (double)' to 'double (__cdecl *)(double)'
    > in this part :
    > double Integration::quad2d(double (*func)(double,double))
    > {
    >
    > nfunc = func ;
    >
    > return qgaus(f1,x1,x2);//error there


    If 'f1' is a non-static member function, you won't be able to convert
    it to a "pointer to function". Pointers to non-static members are not
    compatible with pointers to functions. You need to rethink your code.

    >
    > }
    >
    > //double (*nfunc)(double,double);
    >
    > //double qgaus(double (*f)(double),double min,double max) const;
    >
    > Please help



    V
     
    Victor Bazarov, Feb 17, 2005
    #4
  5. "Sydex" <> skrev i en meddelelse
    news:cv2h71$tv1$-tv.ru...
    > When I compile code I get error
    > C2664: 'Integration::qgaus' : cannot convert parameter 1 from 'double
    > (double)' to 'double (__cdecl *)(double)'


    The errormessage indicates that you should pass a pointer to a function, but
    passed the function itself.

    > in this part :
    > double Integration::quad2d(double (*func)(double,double))
    > {
    >
    > nfunc = func ;
    >
    > return qgaus(f1,x1,x2);//error there


    Try
    return qgaus(&f1,x1,x2);
    instead.

    As an alternative, you might pass the function by reference.

    /Peter
    >
    > }
    >
    > //double (*nfunc)(double,double);
    >
    > //double qgaus(double (*f)(double),double min,double max) const;
    >
    > Please help
    >
    >
     
    Peter Koch Larsen, Feb 17, 2005
    #5
  6. Sydex

    Sydex Guest

    file "integration.h"
    #ifndef INTEGRATION_H_

    #define INTEGRATION_H_

    class Integration

    {

    private:

    double (*nfunc)(double,double);

    double xsav;

    double f1(double);

    double f2(double);

    double x1,y1,x2,y2;

    int accuracy;

    public:

    Integration();

    Integration(int naccuracy,double xx1,double xx2,double yy1,double yy2);

    virtual ~Integration() { }

    double qgaus(double (*f)(double),double min,double max) const;

    double quad2d(double (*func)(double,double));

    void SetIntLim(const double xx1,const double xx2,const double yy1,const
    double yy2);

    void SetAccuracy(const int naccuracy);

    };

    #endif

    file "integration.cpp"

    #include "stdafx.h"

    #include "Integration.h"

    Integration::Integration()

    {

    x1 = 0;

    x2 = 1;

    y1 = 0;

    y2 = 1;

    accuracy = 20;

    }

    Integration::Integration(int naccuracy,double xx1,double xx2,double
    yy1,double yy2)

    {

    x1 = xx1 ;

    x2 = xx2 ;

    y1 = yy1;

    y2 = yy2;

    accuracy = naccuracy;

    }

    void Integration::SetAccuracy(const int naccuracy)

    {

    accuracy = naccuracy;

    }

    void Integration::SetIntLim(const double xx1,const double xx2,const double
    yy1,const double yy2)

    {

    x1 = xx1 ;

    x2 = xx2 ;

    y1 = yy1;

    y2 = yy2;

    }

    double Integration::qgaus(double (*f)(double),double min,double max) const

    {

    double sqrt3 = 1.7320508075688772935;

    double step = (max - min)/accuracy;

    double a,b = min;

    double integral = 0;

    if ((max - min) <= 0)

    {

    return 0 ;

    }

    for (int i=0;i<accuracy;i++)

    {

    a = b;

    b +=step;

    integral += ( (b-a)/2) * ( (*f)(-(b-a)/(2*sqrt3)+ (a+b)/2 ) + (*f)(
    (b-a)/(sqrt3*2) + (a+b)/2 ));

    }

    return integral;

    }

    double Integration::quad2d(double (*func)(double,double))

    {

    nfunc = func ;

    return qgaus(f1,x1,x2);

    }

    double Integration::f1(double x)

    {

    xsav = x ;

    return qgaus(f2,y1,y2);

    }

    double Integration::f2(double y)

    {

    return nfunc(xsav,y);

    }



    "SnaiL" <> wrote in message
    news:...
    > What is f1? Your question is correct, but code you have provide is too
    > bad to understand where is a problem.
    >
     
    Sydex, Feb 17, 2005
    #6
  7. Sydex

    Ron Natalie Guest

    Re: cannot convert parameter from 'double (double)' to 'double (__cdecl*)(double)' error

    Sydex wrote:
    > I forgot
    > // double Integration::f1(double x)
    >
    >

    Is this a non-static member?
     
    Ron Natalie, Feb 17, 2005
    #7
  8. Sydex

    Ron Natalie Guest

    Re: cannot convert parameter from 'double (double)' to 'double (__cdecl*)(double)' error

    Sydex wrote:

    > double f1(double);


    >
    > double qgaus(double (*f)(double),double min,double max) const;
    >
    > return qgaus(f1,x1,x2);
    >

    f1 is a non-static member. You can not convert non-static member
    functions to pointers to regular functions. It would appear
    that your f1 function doesn't depend at all on the Integration
    instances. Just declare it static:

    static double f1(double);
     
    Ron Natalie, Feb 17, 2005
    #8
  9. Sydex

    Matthias Guest

    Re: cannot convert parameter from 'double (double)' to 'double (__cdecl*)(double)' error

    Victor Bazarov wrote:

    > If 'f1' is a non-static member function, you won't be able to convert
    > it to a "pointer to function". Pointers to non-static members are not
    > compatible with pointers to functions. You need to rethink your code.


    Why? Can't he just use an adaptor? Like std::mem_fun_ref or such?

    --
    Regards,
    Matthias
     
    Matthias, Feb 17, 2005
    #9
  10. Re: cannot convert parameter from 'double (double)' to 'double (__cdecl*)(double)' error

    Matthias wrote:
    > Victor Bazarov wrote:
    >
    >> If 'f1' is a non-static member function, you won't be able to convert
    >> it to a "pointer to function". Pointers to non-static members are not
    >> compatible with pointers to functions. You need to rethink your code.

    >
    >
    > Why? Can't he just use an adaptor? Like std::mem_fun_ref or such?


    They are functors. If the target for this adaptation is not a template,
    it won't be able to pick it up. Consider

    int foo(int (*f)()) // not a template
    {
    return f();
    }

    template<class F> int bar(F f) // a template
    {
    return f();
    }

    int fubar() // function
    {
    return 42;
    }

    struct barf // functor (like adaptors)
    {
    int operator()() { return 666; }
    };

    int main()
    {
    foo(fubar); // fine
    bar(fubar); // fine
    barf b;
    foo(b); // error
    bar(b); // fine
    }

    See the recent thread about passing a functor to 'qsort' in c.l.c++.m

    Victor
     
    Victor Bazarov, Feb 17, 2005
    #10
  11. Re: cannot convert parameter from 'double (double)' to 'double (__cdecl*)(double)' error

    Matthias wrote:
    > Victor Bazarov wrote:
    >
    >> If 'f1' is a non-static member function, you won't be able to convert
    >> it to a "pointer to function". Pointers to non-static members are not
    >> compatible with pointers to functions. You need to rethink your code.

    >
    > Why? Can't he just use an adaptor? Like std::mem_fun_ref or such?


    An adaptor would convert member function pointer to functional object.
    The OP needs to convert member function pointer to regular function
    pointer. The latter is cannot be done by standard means in C++. There
    are platform-specific implementations that can do that, but this is a
    completely different story.

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Feb 17, 2005
    #11
  12. Sydex

    Matthias Guest

    Re: cannot convert parameter from 'double (double)' to 'double (__cdecl*)(double)' error

    Andrey Tarasevich wrote:
    > Matthias wrote:
    >
    >>Victor Bazarov wrote:
    >>
    >>
    >>>If 'f1' is a non-static member function, you won't be able to convert
    >>>it to a "pointer to function". Pointers to non-static members are not
    >>>compatible with pointers to functions. You need to rethink your code.

    >>
    >>Why? Can't he just use an adaptor? Like std::mem_fun_ref or such?

    >
    >
    > An adaptor would convert member function pointer to functional object.
    > The OP needs to convert member function pointer to regular function
    > pointer. The latter is cannot be done by standard means in C++. There
    > are platform-specific implementations that can do that, but this is a
    > completely different story.
    >


    Hm, this qgaus function is his own function right? Why not just change
    the first parameter to take a function object instead of a ptr to
    function? ^^

    Am I missing something?


    --
    Regards,
    Matthias
     
    Matthias, Feb 17, 2005
    #12
  13. Re: cannot convert parameter from 'double (double)' to 'double (__cdecl*)(double)' error

    Matthias wrote:
    > [...]
    > Hm, this qgaus function is his own function right? Why not just change
    > the first parameter to take a function object instead of a ptr to
    > function? ^^
    >
    > Am I missing something?


    I think so. If you change it to an object (or a reference), then it
    will be unable to accept function pointers. Unless it is converted
    into a template, there is no accepting both function pointers and
    functors. See my other reply if you haven't already.

    V
     
    Victor Bazarov, Feb 17, 2005
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sagar Choudhary

    Problem with __cdecl

    Sagar Choudhary, Dec 22, 2004, in forum: C++
    Replies:
    6
    Views:
    1,406
    Jay Nabonne
    Dec 22, 2004
  2. Polanski24
    Replies:
    1
    Views:
    571
    Rolf Magnus
    Apr 11, 2005
  3. stdcall and __cdecl

    , Jul 4, 2005, in forum: C++
    Replies:
    3
    Views:
    787
    Larry I Smith
    Jul 4, 2005
  4. kwijibo28
    Replies:
    4
    Views:
    608
    Mike Smith
    Jun 6, 2006
  5. ahong
    Replies:
    0
    Views:
    1,273
    ahong
    Apr 2, 2009
Loading...

Share This Page