references to function in classes

This is a discussion on references to function in classes within the C++ Programming forums, part of the General Programming Boards category; Hey, How can I get this one work? There is a class which gets a function f as an argument. ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    3

    Unhappy references to function in classes

    Hey,
    How can I get this one work?
    There is a class which gets a function f as an argument. This function is combined with other ones to a new function diff. So far it's fine.

    Now I want to pass this new function as a reference for further usage. I just can't get around this error the compiler gives me: " cannot convert ‘double (test::*)(double)’ to ‘double (*)(double)’ in assignment "

    I even don't understand the problem at all.

    Code:
    //provides a function (diff) which gives the difference from an inputfuntion to zero.
    class test
    {
    public:
      test() {diff = &standardFunc_;};
      ~test();
       double (*diff)(double x);
       void setFunc(double f(double x))
      {
        func_ = f;
        diff = &test::calc_; //error:  cannot convert ‘double (test::*)(double)’ to ‘double (*)(double)’ in assignment 
      };
    
    private:
      static double standardFunc_(double x) { return 0;}
      double (*func_)(double x);
      double calc_(double x)
      {
        return func_(x) - standardFunc_(x);
      }	
    };
    I need that for ceating an easy-to-use fit-interface for the gsl-library. For fitting I need to give some functions as refrences - but this doesn't work within a class .

    Hope somebody can help
    Christian

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    http://www.newty.de/fpt/index.html
    Declaring function pointers to class member functions is a bit more involved than what you have at the moment.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    3

    references to function in classes

    thanks, i see... ok, i will read this Tutorial.

    Is there anybody who knows and wants to post an example how to use this class-function instead of any global (static) function?

    greets
    Christian

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    3

    solved

    Thank you for help and suggestions.

    I don't want to declare the method static, because it should be different for every object. Anyway, I've found a solution.

    For the given example:
    I declare a static vector of pointers to methods func_. There every object gets its own entry. Then, instead of using the method diff, I use calc_ with the appropriate method func_.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 09:28 AM
  2. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 02:17 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Inheritance Classes w/ same function name
    By Diamonds in forum C++ Programming
    Replies: 3
    Last Post: 03-11-2003, 01:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21