1

次のコードは、このエラーを返します。

main.cpp|80|エラー: 'T& マトリックス::at(size_t, size_t) [with T = int, size_t = long unsigned int]' の 'this' 引数として 'const matrix' を渡すと、修飾子が破棄されます [-fpermissive]' </p>

それを修復する理由と方法はありますか?

#include <vector>

template <typename T>
class matrix
{
    std::vector< std::vector<T> > data;
    size_t N, M;    
public:
        T& at(size_t x, size_t y);
        matrix<T> operator+(const matrix<T>& m2) const;
};

template<class T>
T& matrix<T>::at(size_t x, size_t y)
{ return data[x][y]; }

template<class T>
matrix<T> matrix<T>::operator+(const matrix<T>& m2) const
{    
    matrix<T> res; //initialization of internals not related to error
    for(unsigned int i=0; i<N; ++i)
        for(unsigned int j=0; j<M; ++j)
            res.at(i,j) = this->at(i, j) + m2.at(i, j);    
    return res;
}

int main()
{
    matrix<int> a, b; //initialization of internals not related to error
    a = a + b;
    return 0;
}

http://ideone.com/5OigTP

4

2 に答える 2

7

at次の場合にもオーバーロードする必要がありconst matrix<>ます。

template<class T>
const T& matrix<T>::at(size_t x, size_t y) const
{
    return data[x][y];
}
于 2012-12-14T19:13:34.630 に答える
1

???のconstオーバーロードを追加してみましたか?at()

のように:

const T& at(size_t x, size_t y) const;
于 2012-12-14T19:14:54.780 に答える