Results 1 to 7 of 7

Thread: What is wrong with this code?

  1. #1
    Join Date
    Apr 2010
    Posts
    77
    Thanks
    10
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X

    Default What is wrong with this code?

    I downloaded some code and I get this error. It looks ok to me.

    Here's the declaration in the .h file.

    Qt Code:
    1. void SetBorder( bool useBorder, QBrush& borderBrush = QBrush() );
    To copy to clipboard, switch view to plain text mode 

    Here's the .cpp code:
    Qt Code:
    1. void JBWidget::SetBorder( bool useBorder, QBrush& borderBrush )
    2. {
    3. .....
    4. }
    To copy to clipboard, switch view to plain text mode 

    Here's the error:
    error: default argument for parameter of type 'QBrush&' has type 'QBrush'

    Why can't it do this?

    Cheers
    Jeff

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: What is wrong with this code?

    Did you include QBrush?

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: What is wrong with this code?

    The default value type must be implicitly convertible to the parameter type. Default values are constants, so you cannot take a non-const reference to one.

    What are you expecting to achieve? By passing the borderBrush parameter by reference you are saying that SetBorder() wants to modify the brush. On the other hand you are saying this parameter is usually just a default value, i.e. the caller usually doesn't provide a value or, by extension, want your modification.

    You could declare it as:
    Qt Code:
    1. void SetBorder( bool useBorder, const QBrush& borderBrush = QBrush() );
    To copy to clipboard, switch view to plain text mode 
    and your compiler will not throw the error. Your method will not be able to modify the brush though.

  4. #4
    Join Date
    Apr 2010
    Posts
    77
    Thanks
    10
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: What is wrong with this code?

    Thanks for the quick response.

    I did include QBrush so I assume its the second reason.

    Although given that I downloaded this code from gitorious as a working example, I'm a bit bewildered why it would have worked for the author and not for me or, alternatively, why they would put up a piece of code that doesn't compile.

    Thanks for the help.

    Cheers
    Jeff

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: What is wrong with this code?

    A lesson I learned many, many years ago... A developer putting something into source control does not mean it compiles, runs, or works as required (or even as designed).

  6. #6
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: What is wrong with this code?

    Also, some compilers will incorrectly create an anonymous variable based on the assignment shown. This is wrong, but it allows such constructs to compile. So it may be that the original author is using a crappy compiler, and doesn't know any better than to avoid writing code like this.
    Last edited by SixDegrees; 18th February 2011 at 07:54.

  7. #7
    Join Date
    Apr 2010
    Posts
    77
    Thanks
    10
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: What is wrong with this code?

    Thanks Chris

    I'll keep that in mind.

    Jeff

Similar Threads

  1. What do I do wrong
    By Arif Bilgin in forum Newbie
    Replies: 12
    Last Post: 20th October 2010, 20:03
  2. Qt Creator Green lines under everything suggesting all my code is wrong
    By Sydius in forum Qt Tools
    Replies: 1
    Last Post: 21st June 2010, 04:57
  3. What is wrong with my code?
    By Dante in forum Qt Programming
    Replies: 27
    Last Post: 6th April 2009, 07:56
  4. Pasting code from code tag in emacs
    By Gopala Krishna in forum General Discussion
    Replies: 0
    Last Post: 16th February 2007, 05:47

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.