c++ compile error: discards qualifier

在调试matrix类的时候,遇到了这个错误。

参考这儿知道这个错误的原因了:

discards qualifier

1. Example myfile.cpp: In function `int main()’: myfile.cpp:20: passing `const DayOfYear’ as `this’ argument of `void DayOfYear::Set(int, int)’ discards qualifiers
2. Meaning
You have an inconsistency with the use of “const”
3. Usual Causes
1. A non-const member function is being invoked for a const object
2. A const object is being passed as a non-const parameter
3. A const member function calls a non-const member function

应该是const的问题,要么是应该是const而没有进行const,或不是const而进行了const。
代码里这两个问题都遇到了。

这儿是修改后的代码,原来的代码都没有进行const定义。


T operator ()(const unsigned long row, const unsigned long col) const;

Matrix operator ~();

Matrix& operator =(const Matrix& rmatrix);

Matrix& operator= (const T& scalar);

Matrix operator -(const T a);

Matrix operator -(const Matrix& matrix);

Matrix& operator -=(const T a);

Matrix& operator -=(const Matrix& matrix);

之前的编译错误为:

includes/matrix.hpp:1126: error: passing ‘const ldas::Matrix‘ as ‘this’ argument of ‘T ldas::Matrix::operator()(long unsigned int, long unsigned int) [with T = double]’ discards qualifiers

若在中文LOCALE下,错误为:

includes/matrix.hpp:1147: 错误: 将 ‘const ldas::Matrix’ 作为 ‘ldas::Matrix ldas::Matrix::operator-() [with T = double]’ 的 ‘this’ 实参时丢弃了类型限定

最终,在调试了对应的const定义后,代码己可以在gcc/g++ 4.1.2下进行编译了。

发表评论

电子邮件地址不会被公开。 必填项已用*标注