Results 1 to 15 of 15

Thread: function pointer (how to pass as parameter)?

  1. #1
    Join Date
    Nov 2009
    Posts
    94
    Thanks
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question 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?

    Qt Code:
    1. class SpectrogramData: public QwtRasterData
    2. {
    3. public:
    4. SpectrogramData(double (*b)(double, double), double rang1[2], double rang2[2]):
    5. QwtRasterData(QwtDoubleRect(rang1[0], rang2[0], rang1[1]- rang1[0], rang2[1]- rang2[0]))
    6. {
    7. benchFunc = b;
    8. rangeX1[0] = rang1[0]; rangeX1[1] = rang1[1];
    9. rangeX2[0] = rang1[0]; rangeX2[1] = rang2[1];
    10. }
    11.  
    12. virtual QwtRasterData *copy() const
    13. {
    14. return new SpectrogramData( benchFunc , &rangeX1[2], &rangeX2[2]); //<<< << this is the reason for the error
    15. }
    16.  
    17. virtual QwtDoubleInterval range() const
    18. {
    19. return QwtDoubleInterval(0.0, 10.0);
    20. }
    21.  
    22. virtual double value(double x, double y) const
    23. {
    24.  
    25. const double result = benchFunc(x,y);
    26. return result;
    27. }
    28.  
    29. private:
    30. double (*benchFunc)(double, double); // function pointer
    31. double rangeX1[2];
    32. double rangeX2[2];
    33. };
    To copy to clipboard, switch view to plain text mode 

    error:
    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

  2. #2
    Join Date
    Aug 2009
    Posts
    92
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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:
    Qt Code:
    1. typedef double (*benchFunc)(double, double);
    To copy to clipboard, switch view to plain text mode 

    now you have defined that the type benchFunc is a pointer to a function that returns double and gets 2 doubles.

    then:
    Qt Code:
    1. benchFunc pointerName;
    To copy to clipboard, switch view to plain text mode 

    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 &:
    Qt Code:
    1. return new SpectrogramData( &SpectrogramData::pointerName, &rangeX1[2], &rangeX2[2]);
    To copy to clipboard, switch view to plain text mode 


    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:
    Qt Code:
    1. pointerName = &SpectrogramData::functionName;
    To copy to clipboard, switch view to plain text mode 
    Last edited by trallallero; 30th November 2009 at 15:27.

  3. #3
    Join Date
    Nov 2009
    Posts
    94
    Thanks
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: function pointer (how to pass as parameter)?

    If I try out your code, I get the following error:
    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:
    Qt Code:
    1. typedef double (*benchFunc)(double, double);
    To copy to clipboard, switch view to plain text mode 
    and where
    Qt Code:
    1. benchFunc pointerName;
    To copy to clipboard, switch view to plain text mode 
    ?

  4. #4
    Join Date
    Aug 2009
    Posts
    92
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: function pointer (how to pass as parameter)?

    Define it in the header file, before the class definition and this:
    Qt Code:
    1. benchFunc pointerName;
    To copy to clipboard, switch view to plain text mode 

    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).

  5. #5
    Join Date
    Aug 2009
    Posts
    92
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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
    Qt Code:
    1. g++ test.cpp -o test
    To copy to clipboard, switch view to plain text mode 
    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


    Qt Code:
    1. #include <stdio.h>
    2.  
    3. typedef double (*benchFunc)(double, double); // definition of function pointer
    4.  
    5. class SpectrogramData
    6. {
    7. public:
    8. SpectrogramData(double (*b)(double, double), double rang1[2], double rang2[2])
    9. {
    10. m_pFunc = b;
    11. rangeX1[0] = rang1[0]; rangeX1[1] = rang1[1];
    12. rangeX2[0] = rang1[0]; rangeX2[1] = rang2[1];
    13. }
    14.  
    15. virtual double value(double x, double y) const
    16. {
    17. const double result = m_pFunc(x,y);
    18. return result;
    19. }
    20.  
    21. private:
    22. benchFunc m_pFunc; // function pointer
    23.  
    24. double rangeX1[2];
    25. double rangeX2[2];
    26. };
    27.  
    28. double testFunc(double d1, double d2)
    29. {
    30. return d1 + d2;
    31. }
    32.  
    33. int main()
    34. {
    35. double rang1[2] = {1.1, 2.2};
    36. double rang2[2] = {3.3, 4.4};
    37.  
    38. SpectrogramData sd(&testFunc, rang1, rang2);
    39.  
    40. double res = sd.value(1.1, 2.2);
    41.  
    42. printf("res: <%lf>\n", res);
    43.  
    44. return 0;
    45. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Nov 2009
    Posts
    94
    Thanks
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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:

    Qt Code:
    1. SpectrogramData(double (SpectrogramData::*b)(double, double), double rang1[2], double rang2[2]);
    To copy to clipboard, switch view to plain text mode 
    but I want to call it with:
    Qt Code:
    1. SpectrogramData(double (*b)(double, double), double rang1[2], double rang2[2]);
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Nov 2009
    Posts
    94
    Thanks
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: function pointer (how to pass as parameter)?

    Quote Originally Posted by trallallero View Post
    I've done a little example in a file called test.cpp, with your code, compiled with
    Qt Code:
    1. g++ test.cpp -o test
    To copy to clipboard, switch view to plain text mode 
    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


    Qt Code:
    1. #include <stdio.h>
    2.  
    3. typedef double (*benchFunc)(double, double); // definition of function pointer
    4.  
    5. class SpectrogramData
    6. {
    7. public:
    8. SpectrogramData(double (*b)(double, double), double rang1[2], double rang2[2])
    9. {
    10. m_pFunc = b;
    11. rangeX1[0] = rang1[0]; rangeX1[1] = rang1[1];
    12. rangeX2[0] = rang1[0]; rangeX2[1] = rang2[1];
    13. }
    14.  
    15. virtual double value(double x, double y) const
    16. {
    17. const double result = m_pFunc(x,y);
    18. return result;
    19. }
    20.  
    21. private:
    22. benchFunc m_pFunc; // function pointer
    23.  
    24. double rangeX1[2];
    25. double rangeX2[2];
    26. };
    27.  
    28. double testFunc(double d1, double d2)
    29. {
    30. return d1 + d2;
    31. }
    32.  
    33. int main()
    34. {
    35. double rang1[2] = {1.1, 2.2};
    36. double rang2[2] = {3.3, 4.4};
    37.  
    38. SpectrogramData sd(&testFunc, rang1, rang2);
    39.  
    40. double res = sd.value(1.1, 2.2);
    41.  
    42. printf("res: <%lf>\n", res);
    43.  
    44. return 0;
    45. }
    To copy to clipboard, switch view to plain text mode 


    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

  8. #8
    Join Date
    Aug 2009
    Posts
    92
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: function pointer (how to pass as parameter)?

    Ok, I understand that it's the first time you use pointers to functions

    The only difference between this (from your code, a class member)
    Qt Code:
    1. double (*benchFunc)(double, double); // function pointer
    To copy to clipboard, switch view to plain text mode 

    and my
    Qt Code:
    1. typedef double (*benchFunc)(double, double); // definition of function pointer
    2. benchFunc m_pFunc; // function pointer
    To copy to clipboard, switch view to plain text mode 
    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:
    Qt Code:
    1. SpectrogramData sd(&testFunc, rang1, rang2);
    To copy to clipboard, switch view to plain text mode 
    where testFunc is the function passed to the SpectrogramData.

  9. #9
    Join Date
    Aug 2009
    Posts
    92
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: function pointer (how to pass as parameter)?

    Quote Originally Posted by rambo83 View Post
    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

    Once I've done a state machine with a matrix of pointers to functions

    Ciao

  10. The following user says thank you to trallallero for this useful post:

    rambo83 (30th November 2009)

  11. #10
    Join Date
    Nov 2009
    Posts
    94
    Thanks
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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)

    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:

    Qt Code:
    1. class SpectrogramData: public QwtRasterData
    2. {
    3. public:
    4. SpectrogramData(double (*b)(double, double), double rang1[2], double rang2[2]):
    5. QwtRasterData(QwtDoubleRect(rang1[0], rang2[0], rang1[1]- rang1[0], rang2[1]- rang2[0]))
    6. {
    7. benchFunc = b;
    8. rangeX1[0] = rang1[0]; rangeX1[1] = rang1[1];
    9. rangeX2[0] = rang1[0]; rangeX2[1] = rang2[1];
    10. }
    11.  
    12. virtual QwtRasterData *copy() const
    13. {
    14. return new SpectrogramData( benchFunc , &rangeX1[2], &rangeX2[2]);
    15. }
    16.  
    17. virtual QwtDoubleInterval range() const
    18. {
    19. return QwtDoubleInterval(0.0, 10.0);
    20. }
    21.  
    22. virtual double value(double x, double y) const
    23. {
    24.  
    25. const double result = benchFunc(x,y);
    26. return result;
    27. }
    28.  
    29. private:
    30. double (*benchFunc)(double, double); // function pointer
    31. double rangeX1[2];
    32. double rangeX2[2];
    33. };
    To copy to clipboard, switch view to plain text mode 

  12. #11
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,309
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default 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) ...

    Qt Code:
    1. SpectrogramData(double (*b)(double, double), const double rang1[2], const double rang2[2]):
    2. QwtRasterData(QwtDoubleRect(rang1[0], rang2[0], rang1[1]- rang1[0], rang2[1]- rang2[0]))
    3. {
    4. ...
    5. }
    6.  
    7. virtual QwtRasterData *copy() const
    8. {
    9. return new SpectrogramData( benchFunc , rangeX1, rangeX2);
    10. }
    11.  
    12. ...
    To copy to clipboard, switch view to plain text mode 

    Why not trying to understand the messages of the compiler yourself ?

    Uwe

  13. The following user says thank you to Uwe for this useful post:

    rambo83 (30th November 2009)

  14. #12
    Join Date
    Nov 2009
    Posts
    94
    Thanks
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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 &);

    Qt Code:
    1. class SpectrogramData: public QwtRasterData
    2. {
    3. public:
    4. SpectrogramData(double (*b)(double, double), double rang1[2], double rang2[2]):
    5. QwtRasterData(QwtDoubleRect(rang1[0], rang2[0], rang1[1]- rang1[0], rang2[1]- rang2[0]))
    6. {
    7. benchFunc = b;
    8. rangeX1[0] = rang1[0]; rangeX1[1] = rang1[1];
    9. rangeX2[0] = rang1[0]; rangeX2[1] = rang2[1];
    10. itself = this;
    11. }
    12.  
    13. virtual QwtRasterData *copy() const
    14. {
    15. return new SpectrogramData( *itself); //<<< << this is the reason for the error
    16. }
    17.  
    18. virtual QwtDoubleInterval range() const
    19. {
    20. return QwtDoubleInterval(0.0, 10.0);
    21. }
    22.  
    23. virtual double value(double x, double y) const
    24. {
    25.  
    26. const double result = benchFunc(x,y);
    27. return result;
    28. }
    29.  
    30. private:
    31. double (*benchFunc)(double, double); // function pointer
    32. double rangeX1[2];
    33. double rangeX2[2];
    34. SpectrogramData *itself;
    35. };
    To copy to clipboard, switch view to plain text mode 

  15. #13
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default 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".

    Qt Code:
    1. virtual QwtRasterData *copy() const
    2. {
    3. return new SpectrogramData( *this ); //<< this is what you could do instead
    4. }
    To copy to clipboard, switch view to plain text mode 

    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.

  16. The following user says thank you to d_stranz for this useful post:

    rambo83 (1st December 2009)

  17. #14
    Join Date
    Nov 2009
    Posts
    94
    Thanks
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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

  18. #15
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: function pointer (how to pass as parameter)?

    Quote Originally Posted by rambo83 View Post
    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

Similar Threads

  1. QPSQL problem
    By LoneWolf in forum Installation and Deployment
    Replies: 60
    Last Post: 4th November 2009, 15:22
  2. Regading Driver to connect Postgresql Database
    By dummystories in forum Installation and Deployment
    Replies: 38
    Last Post: 12th March 2009, 08:19
  3. QPSQL driver in windows
    By brevleq in forum Installation and Deployment
    Replies: 31
    Last Post: 14th December 2007, 13:57
  4. how to add static library into qmake
    By Namrata in forum Qt Tools
    Replies: 1
    Last Post: 20th November 2007, 18:33
  5. use qpsql
    By raphaelf in forum Installation and Deployment
    Replies: 34
    Last Post: 22nd August 2006, 13:52

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.