Results 1 to 6 of 6

Thread: Linker error when using qMin(), qMax(), qBound()

  1. #1

    Unhappy Linker error when using qMin(), qMax(), qBound()

    Trying to use static const members in functions qMin(), qMax(), qBound() gives a linker error:
    undefined reference to `<the static constant>'.

    Here is an example code:
    Qt Code:
    1. class Test
    2. {
    3. public:
    4. static const float KValue=3.0;
    5. Test();
    6. };
    7.  
    8. Test::Test()
    9. {
    10. // Can be any function, not only a constructor
    11. float foo = 1.0;
    12. foo = qMax( foo, KValue );
    13. }
    14.  
    15. int main()
    16. {
    17. // somewhere in a program
    18. Test obj;
    19. }
    To copy to clipboard, switch view to plain text mode 

    On the other hand, when using usual local constants, qMax (and friends) works ok:

    Qt Code:
    1. float foo = 1.0;
    2. const float value = KValue; // Make a copy of the static constant
    3.  
    4. foo = qMax( foo, value );
    To copy to clipboard, switch view to plain text mode 

    Please write, if someone knowswhy this happens or any good workarounds.

    Sytem info:
    Kubuntu 8.04
    Qt 4.4.0 and 4.4.1
    gcc 4.2.4

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Linker error when using qMin(), qMax(), qBound()

    You forgot something:
    Qt Code:
    1. class Test
    2. {
    3. public:
    4. static const float KValue;
    5. Test();
    6. };
    7.  
    8. const float Test::KValue=3.0; //<---
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  3. #3

    Default Re: Linker error when using qMin(), qMax(), qBound()

    Year, right, thanks.
    Static const float members are not allowed to be initialized within the class declaration.
    But why did then the second variant work?

  4. #4
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Linker error when using qMin(), qMax(), qBound()

    The problem was not with qMax() etc but with your constant.
    If you declare a class member (variable) as static, then you have to define it (outside of the class body). Otherwise the linker will complain.

    In your second case, you did not define a static class member, so the linker was happy.

  5. #5

    Default Re: Linker error when using qMin(), qMax(), qBound()

    Nope, in my second example I keep using the same static const float. It's initialized inside the class and used very well inside the later code, but not in function qMax. I am already beginning to suspect that the error occurs because qMax() takes its arguments by reference (not value). Something wrong, when passing address of a static constant to a function.

    Here is the same example 2, but at its full glory. It works, no error here:

    Qt Code:
    1. class Test
    2. {
    3. public:
    4. static const float KValue=3.0;
    5. Test();
    6. };
    7.  
    8. Test::Test()
    9. {
    10. // Can be any function, not only a constructor
    11. float foo = 1.0;
    12. const float value = KValue; // Make a copy of the static constant
    13. foo = qMax( foo, value );
    14. }
    15.  
    16. int main()
    17. {
    18. // somewhere in a program
    19. Test obj;
    20. }
    To copy to clipboard, switch view to plain text mode 

    I've also noticed the following facts:
    * Changing from float to int doesn't change the situation a bit.
    * Using "normal" functions that take arguments by value (not by reference) doesn't lead to the error. Such functions can easily take static constants as arguments.

  6. #6
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Linker error when using qMin(), qMax(), qBound()

    Probably the compiler optimizes your use of that "static const" away. (As it is static const, it is guaranteed to never change and can be replaced by its compile time value).
    (Try without optimizations...)

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
  •  
Qt is a trademark of The Qt Company.