Results 1 to 6 of 6

Thread: How to concatenate << operator on an instance of MY_object (discards qualifiers error

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default How to concatenate << operator on an instance of MY_object (discards qualifiers error

    I have a class named W_debug. I want to use it at my_oher_class.

    I declare it at my_other_class.h
    Qt Code:
    1. W_debug w_debug;
    To copy to clipboard, switch view to plain text mode 
    and / or ( To try if it runs...)
    Qt Code:
    1. W_debug *w_debug
    To copy to clipboard, switch view to plain text mode 
    ;

    But the line :
    Qt Code:
    1. w_debug<<"hello"<<"how areyou";
    To copy to clipboard, switch view to plain text mode 
    Does not work, compiler gives me the error :

    passing 'const W_debug' as 'this' argument of 'W_debug& W_debug:perator<<(const char*)' discards qualifiers

    This is my code :
    (It works fine if I use it using W_debug()<< , that is to say : without create the instance)

    Qt Code:
    1. class W_debug
    2. {
    3. public:
    4. W_debug();
    5. ~W_debug();
    6.  
    7. W_debug & operator + (int data);
    8. W_debug & operator + (const char data);
    9. W_debug & operator + (const char * data);
    10. W_debug & operator + (double data);
    11. .....
    To copy to clipboard, switch view to plain text mode 


    And :

    Qt Code:
    1. W_debug::W_debug() { }
    2. W_debug::~W_debug(){ }
    3.  
    4. W_debug& W_debug::operator << (const char * data) {
    5. os<<data;
    6. return *this;
    7. ......
    8.  
    9. }
    To copy to clipboard, switch view to plain text mode 

    PS:
    I can do something like what I want with 'w_debug' with stringstream :
    Qt Code:
    1. stringstream os;
    2. os <<data1<<data2;
    To copy to clipboard, switch view to plain text mode 
    I want to do something similar



    Any help ? Thanks
    Last edited by tonnot; 29th March 2011 at 19:33.

  2. #2
    Join Date
    Feb 2011
    Posts
    8
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: How to concatenate << operator on an instance of MY_object (discards qualifiers e

    Are you storing the w_debug object in another object? If so, it will inherit the const-ness of that object. Somewhere, the object is getting made const, and that's your problem.

    Also, I'm guessing you're using Visual C++? W_Debug() << "whatevs" should fail for the same reason, but a MS extention allows such constructs.

Similar Threads

  1. QTextStream operator>> and error detection
    By olidem in forum Qt Programming
    Replies: 7
    Last Post: 27th July 2012, 12:38
  2. Replies: 2
    Last Post: 22nd June 2010, 21:35
  3. Replies: 0
    Last Post: 12th November 2009, 18:47
  4. const QStringList discards qualifiers
    By kandalf in forum Qt Programming
    Replies: 2
    Last Post: 25th January 2009, 18:09
  5. Replies: 2
    Last Post: 20th May 2006, 12:45

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.