Ich verstehe diese Compiler-Fehler nicht
-
Folgendes habe ich geschrieben (Ausschnitt):
Matrix.hstatic Matrix<T> sumCols( const Matrix<T> &M ) { Matrix<T> sumVec( M.rows, 1 ); for( unsigned int i = 0; i < M.cols; ++i ) sumVec.setElement( 0, i, 0 ); for( unsigned int j = 0; j < M.cols; ++j ) for( unsigned int i = 0; i < M.rows; ++i ) sumVec.setElement( sumVec.getElement( i,0 ) + M.getElement(i,j), i, 0 ); //Zeile 165 return sumVec; }Und so rufe ich das Teil auf:
Matrix<int> summedAttributes = Matrix<int>::sumCols( _10_Sec_Attribute_Matrix );_10_Sec_Attribute_Matrix ist ein Objekt vom Typ "Matrix<int>".
Und der Compiler moniert folgendes:
g++ -g -Wall -finstrument-functions -c ProcessData.cpp -o ProcessData.o
Matrix.h: In static member function ‘static Matrix<T> Matrix<T>::sumCols(const Matrix<T>&) [with T = int]’:
ProcessData.cpp:19: instantiated from here
Matrix.h:165: error: passing ‘const Matrix<int>’ as ‘this’ argument of ‘T Matrix<T>::getElement(unsigned int, unsigned int) [with T = int]’ discards qualifiers
make: *** [ProcessData.o] Error 1Kann ich nix mit anfangen.

-
getElement ist keine const-Methode, du rufst sie aber auf M (= const-Referenz) auf.
Steht zwar auch genauso in der Fehlermeldung, aber vielleicht hilft es ja.

-
Bashar schrieb:
getElement ist keine const-Methode, du rufst sie aber auf M (= const-Referenz) auf.
Steht zwar auch genauso in der Fehlermeldung, aber vielleicht hilft es ja.

Ich weiß noch dass mich diese Meldung früher auch oft verwirrt hat. Discards qualifiers ist doch recht allgemein gehalten

template<typename T> class Matrix { public: T getElement(size_t i, size_t j) const; };
-
Japp, des wors. Besten Dank.
