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
and / or ( To try if it runs...)
;
But the line :
Code:
w_debug<<"hello"<<"how areyou";
Does not work, compiler gives me the error :
passing 'const W_debug' as 'this' argument of 'W_debug& W_debug::operator<<(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)
Code:
class W_debug
{
public:
W_debug();
~W_debug();
W_debug & operator + (int data);
W_debug & operator + (const char data);
W_debug & operator + (const char * data);
W_debug & operator + (double data);
.....
And :
Code:
W_debug::W_debug() { }
W_debug::~W_debug(){ }
W_debug& W_debug::operator << (const char * data) {
os<<data;
return *this;
......
}
PS:
I can do something like what I want with 'w_debug' with stringstream :
Code:
stringstream os;
os <<data1<<data2;
I want to do something similar
Any help ? Thanks
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.
Re: How to concatenate << operator on an instance of MY_object (discards qualifiers e
No, QT.
And, I' simply create the instance.
Internally I use a private os stringstream to save the data.
And... How can I create the w_debug using * ?
Code:
W_debug *w_debug
w_debug<< "hello" // does not work ...
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.
Code:
#include <QApplication>
#include <QHeaderView>
#include <QTableView>
#include "citymodel.h"
int main(int argc, char *argv[])
{
cities << "Arvika" << "Boden" ;
// << "Eskilstuna" << "Falun"
// << "Filipstad" << "Halmstad" << "Helsingborg" << "Karlstad"
// << "Kiruna" << "Kramfors" << "Motala" << "Sandviken"
// << "Skara" << "Stockholm" << "Sundsvall" << "Trelleborg";
CityModel cityModel;
cityModel.setCities(cities);
tableView.setModel(&cityModel);
tableView.setAlternatingRowColors(true);
tableView.
setWindowTitle(QObject::tr("Cities"));
tableView.show();
return app.exec();
}
citymodel.h
Code:
#ifndef CITYMODEL_H
#define CITYMODEL_H
#include <QAbstractTableModel>
#include <QStringList>
#include <QVector>
#include <w_debug.h>
{
Q_OBJECT
public:
int role);
QVariant headerData
(int section, Qt
::Orientation orientation,
int role) const;
private:
int offsetOf(int row, int column) const;
QVector<int> distances;
W_debug w_debug;
};
#endif
citymodel.cpp
Code:
#include <QtCore>
#include "citymodel.h"
CityModel
::CityModel(QObject *parent
){
w_debug.modo_log_start("c:/file.log");
}
void CityModel
::setCities(const QStringList &cityNames
) {
cities = cityNames;
distances.resize(cities.count() * (cities.count() - 1) / 2);
distances.fill(0);
reset();
}
int CityModel
::rowCount(const QModelIndex & /* parent */) const {
std::string a="row count";
w_debug<<a;
return cities.count();
}
int CityModel
::columnCount(const QModelIndex & /* parent */) const {
w_debug<<"col count"<<"";
return cities.count();
}
{
w_debug<<"data"<<index.row()<<" "<<index.column()<<"";
if (!index.isValid())
if (role == Qt::TextAlignmentRole) {
return int(Qt::AlignRight | Qt::AlignVCenter);
} else if (role == Qt::DisplayRole) {
if (index.row() == index.column())
return 0;
int offset = offsetOf(index.row(), index.column());
return distances[offset];
}
}
{
w_debug<<"setdata"<<index.row()<<" " <<index.column();
if (index.isValid() && index.row() != index.column()
&& role == Qt::EditRole) {
int offset = offsetOf(index.row(), index.column());
distances[offset] = value.toInt();
QModelIndex transposedIndex
= createIndex
(index.
column(),
index.row());
// emit dataChanged(index, index);
// emit dataChanged(transposedIndex, transposedIndex);
return true;
}
return false;
}
QVariant CityModel
::headerData(int section,
Qt::Orientation orientation ,
int role) const
{
w_debug<<"headerdata"<<section<<" "<<orientation;
if (role == Qt::DisplayRole)
return cities[section];
}
Qt
::ItemFlags CityModel
::flags(const QModelIndex &index
) const{
w_debug<<"flags"<<index.row()<<" " <<index.column();
if (index.row() != index.column())
flags |= Qt::ItemIsEditable;
return flags;
}
int CityModel::offsetOf(int row, int column) const
{
if (row < column)
qSwap(row, column);
return (row * (row - 1) / 2) + column;
}
All w_debug's << , gives me the error (discards qualifiers .....
Re: How to concatenate << operator on an instance of MY_object (discards qualifiers e
Quote:
Originally Posted by
tonnot
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
Code:
{
...
W_debug w_debug;
};
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.
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 :
Code:
{ ....
return value}
.... 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.
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.