Results 1 to 6 of 6

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

  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.

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

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

    No, QT.
    And, I' simply create the instance.
    Qt Code:
    1. W_debug w_debug;
    To copy to clipboard, switch view to plain text mode 
    Internally I use a private os stringstream to save the data.

    And... How can I create the w_debug using * ?
    Qt Code:
    1. W_debug *w_debug
    2. w_debug<< "hello" // does not work ...
    To copy to clipboard, switch view to plain text mode 
    error : no match for 'operator<<' in '&((const CityModel*)this)->CityModel::w_debug

    I dont undertand it .....


    Added after 14 minutes:


    New information :
    I can use w_debug as I want in another project, but not in this one:
    (It is an example of QT Gui programming)

    main.
    Qt Code:
    1. #include <QApplication>
    2. #include <QHeaderView>
    3. #include <QTableView>
    4.  
    5. #include "citymodel.h"
    6.  
    7. int main(int argc, char *argv[])
    8. {
    9. QApplication app(argc, argv);
    10.  
    11. QStringList cities;
    12. cities << "Arvika" << "Boden" ;
    13. // << "Eskilstuna" << "Falun"
    14. // << "Filipstad" << "Halmstad" << "Helsingborg" << "Karlstad"
    15. // << "Kiruna" << "Kramfors" << "Motala" << "Sandviken"
    16. // << "Skara" << "Stockholm" << "Sundsvall" << "Trelleborg";
    17.  
    18. CityModel cityModel;
    19. cityModel.setCities(cities);
    20.  
    21. QTableView tableView;
    22. tableView.setModel(&cityModel);
    23. tableView.setAlternatingRowColors(true);
    24. tableView.setWindowTitle(QObject::tr("Cities"));
    25. tableView.show();
    26.  
    27. return app.exec();
    28. }
    To copy to clipboard, switch view to plain text mode 

    citymodel.h
    Qt Code:
    1. #ifndef CITYMODEL_H
    2. #define CITYMODEL_H
    3.  
    4. #include <QAbstractTableModel>
    5. #include <QStringList>
    6. #include <QVector>
    7. #include <w_debug.h>
    8.  
    9. class CityModel : public QAbstractTableModel
    10. {
    11. Q_OBJECT
    12.  
    13. public:
    14. CityModel(QObject *parent = 0);
    15.  
    16. void setCities(const QStringList &cityNames);
    17. int rowCount(const QModelIndex &parent) const;
    18. int columnCount(const QModelIndex &parent) const;
    19. QVariant data(const QModelIndex &index, int role) const;
    20. bool setData(const QModelIndex &index, const QVariant &value,
    21. int role);
    22. QVariant headerData(int section, Qt::Orientation orientation,
    23. int role) const;
    24. Qt::ItemFlags flags(const QModelIndex &index) const;
    25.  
    26. private:
    27. int offsetOf(int row, int column) const;
    28.  
    29. QStringList cities;
    30. QVector<int> distances;
    31. W_debug w_debug;
    32.  
    33. };
    34.  
    35. #endif
    To copy to clipboard, switch view to plain text mode 

    citymodel.cpp
    Qt Code:
    1. #include <QtCore>
    2.  
    3. #include "citymodel.h"
    4.  
    5.  
    6.  
    7. CityModel::CityModel(QObject *parent)
    8. {
    9. w_debug.modo_log_start("c:/file.log");
    10.  
    11.  
    12. }
    13.  
    14.  
    15.  
    16. void CityModel::setCities(const QStringList &cityNames)
    17. {
    18. cities = cityNames;
    19. distances.resize(cities.count() * (cities.count() - 1) / 2);
    20. distances.fill(0);
    21. reset();
    22. }
    23.  
    24. int CityModel::rowCount(const QModelIndex & /* parent */) const
    25. {
    26. std::string a="row count";
    27. w_debug<<a;
    28. return cities.count();
    29. }
    30.  
    31. int CityModel::columnCount(const QModelIndex & /* parent */) const
    32. {
    33. w_debug<<"col count"<<"";
    34. return cities.count();
    35. }
    36.  
    37. QVariant CityModel::data(const QModelIndex &index, int role) const
    38. {
    39. w_debug<<"data"<<index.row()<<" "<<index.column()<<"";
    40. if (!index.isValid())
    41. return QVariant();
    42.  
    43. if (role == Qt::TextAlignmentRole) {
    44. return int(Qt::AlignRight | Qt::AlignVCenter);
    45. } else if (role == Qt::DisplayRole) {
    46. if (index.row() == index.column())
    47. return 0;
    48. int offset = offsetOf(index.row(), index.column());
    49. return distances[offset];
    50. }
    51. return QVariant();
    52. }
    53.  
    54. bool CityModel::setData(const QModelIndex &index,
    55. const QVariant &value, int role)
    56. {
    57. w_debug<<"setdata"<<index.row()<<" " <<index.column();
    58.  
    59. if (index.isValid() && index.row() != index.column()
    60. && role == Qt::EditRole) {
    61. int offset = offsetOf(index.row(), index.column());
    62. distances[offset] = value.toInt();
    63.  
    64. QModelIndex transposedIndex = createIndex(index.column(),
    65. index.row());
    66. // emit dataChanged(index, index);
    67. // emit dataChanged(transposedIndex, transposedIndex);
    68. return true;
    69. }
    70. return false;
    71. }
    72.  
    73. QVariant CityModel::headerData(int section,
    74. Qt::Orientation orientation ,
    75. int role) const
    76. {
    77.  
    78. w_debug<<"headerdata"<<section<<" "<<orientation;
    79. if (role == Qt::DisplayRole)
    80. return cities[section];
    81. return QVariant();
    82. }
    83.  
    84. Qt::ItemFlags CityModel::flags(const QModelIndex &index) const
    85. {
    86. w_debug<<"flags"<<index.row()<<" " <<index.column();
    87. Qt::ItemFlags flags = QAbstractItemModel::flags(index);
    88. if (index.row() != index.column())
    89. flags |= Qt::ItemIsEditable;
    90. return flags;
    91. }
    92.  
    93. int CityModel::offsetOf(int row, int column) const
    94. {
    95. if (row < column)
    96. qSwap(row, column);
    97. return (row * (row - 1) / 2) + column;
    98. }
    To copy to clipboard, switch view to plain text mode 

    All w_debug's << , gives me the error (discards qualifiers .....
    Last edited by tonnot; 29th March 2011 at 20:05.

  4. #4
    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

    Quote Originally Posted by tonnot View Post
    No, QT.
    But your build chain, what are you using? `W_debug() << "blah";`, given your definition of operator<< is not legal C++.
    Quote Originally Posted by tonnot View Post
    Qt Code:
    1. class CityModel : public QAbstractTableModel
    2. {
    3. ...
    4. W_debug w_debug;
    5. };
    To copy to clipboard, switch view to plain text mode 
    All w_debug's << , gives me the error (discards qualifiers .....
    The w_debug object is stored within a CityModel object, and as it turns out you happen to only use operator<< within const methods of CityModel. Within a const method, all members of your CityModel object, including your W_debug, are also const. You can't call non-const methods of const objects, or you get the error you describe. The C++ FAQ Lite has a lot of useful information about this subject.

    There are several ways you can fix this. The easiest is probably just to declare the w_debug member as mutable; this marks it as okay to treat as non-const within const methods. Since the debug output isn't a part of the model's external behavior, this is an acceptable use of mutable. Of course, w_debug probably has no business being a member of CityModel to begin with; I would recommend creating one on demand, or use a global of some sort.

  5. The following user says thank you to conner686 for this useful post:

    tonnot (30th March 2011)

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

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

    Thank connner. I review the code and I understand what you say.
    A last question , I dont understand which is the goal of to have functions with const sufix
    In this function :
    Qt Code:
    1. QVariant CityModel::data(const QModelIndex &index, int role) const
    2. { ....
    3. return value}
    To copy to clipboard, switch view to plain text mode 
    .... I dont understand the meaning of to have const for it....
    If you can explain to me .... (I promise you that I dont understand nothing related with it I have found on the web)
    Thanks.

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

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

    It means the method guarantees that it won't change any of the object's data. In other words you might say it can be called on a "read-only" (const) object. For instance if you pass a const reference to some object into some function, the function can only call const methods of the object, it may not call any "setters", only "getters". In practice it allows the compiler to not do a copy of an object that is passed into some context so that its "value" remains constant (hence "const") outside this context. This is also what makes Qt's implicit sharing tick.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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.