This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Inconsisten error messages on const pointers
- From: Arthur Schwarz <aschwarz1309 at att dot net>
- To: gcc-help at gcc dot gnu dot org
- Date: Sun, 14 Oct 2012 13:33:12 -0700 (PDT)
- Subject: Inconsisten error messages on const pointers
In the example below the following two statements seem to be inconsistent:
1: cout << " " << endl << cell1->dump();
2: this->message += cell1->dump();
1: produces the error message
SlipException.h: In member function âvoid slip::SlipException::log()â:
In file included from SlipCell.h:49:0,
from SlipDatum.h:269,
from SlipOp.h:22,
from SlipStringOp.h:12,
from SlipStringOP.cpp:11:
SlipException.h:29:79: error: passing âconst slip::SlipCellBaseâ as âthisâ
argument of âvirtual std::string slip::SlipCellBase::dump()â discards qualifiers
With 2: there is no diagnostic message. Any reason why?
===============================================================
[code]
# include <exception>
# include <string>
# include <iostream>
# include <stdlib.h>
# include "SlipDef.h"
# include "SlipErr.h"
# include "SlipCellBase.h"
namespace slip {
class SlipException : public exception {
private:
void log() { cout << "error " << message;
SlipCellBase* castCell1 = const_cast<SlipCellBase*>(cell1);
SlipCellBase* castCell2 = const_cast<SlipCellBase*>(cell2);
if (cell1 != NULL) cout << " " << endl <<
cell1->dump(); // error
if (cell2 != NULL) cout << " " << endl <<
castCell2->dump();
cout << endl << flush;
}
protected:
const SlipCellBase* cell1;
const SlipCellBase* cell2;
string message;
SlipException() { };
public:
SlipException(SlipErr::Error message, SlipCellBase* cell1)
: message(constructMessage(message, "", ""))
, cell1(cell1)
, cell2(NULL) {
this->message += cell1->dump(); // no error
log();
}; // SlipException(SlipErr::Error message, SlipCellBase* cell1)
virtual const char* what() const throw() {
return message.c_str();
}; // virtual const char* what() const throw();
~SlipException() throw() { }
}; // class SlipException
[/code]