Results 1 to 6 of 6

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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1

    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.

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

    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.