我想实现一个使用operator [] []。
的矩阵类我编写了下面的代码,但问题是我无法将 const 值发送到operator + !!(错误:将'const Matrix'作为'Matrix :: Indexer Matrix的'this'参数传递: :operator'丢弃限定符[-fpermissive])
如果我将其更改为Matrix operator +(Matrix& other);它工作得很好......
我想我需要两个重载的Matrix :: Indexer Matrix :: operator [](int index)一个用于读取,一个用于写入mat_(就像c#中的属性一样)但是怎么样?!
或者我应该使用const_cast?!
实现此课程的最佳方法是什么?!
//Matrix.h
class Matrix
{
public:
Matrix(const int rows, const int cols, float defaultValue=0);
//TODO copy constructor , destructor
int rows() const;
int cols() const;
Matrix operator+(const Matrix& other);
class Indexer
{
public:
Indexer(float* arr,int cols);
float& operator[](int index);
private:
float* arr_;
int cols_;
};
Indexer operator[](int index);
private:
int rows_;
int cols_;
float **mat_;
};
Matrix.cpp
#include "matrix.h"
Matrix::Matrix(const int rows, const int cols, float defaultValue) :
rows_(rows),
cols_(cols)
{
mat_=new float*[rows_];
for(int i=0;i<rows;i++)
{
mat_[i]=new float[cols];
}
for(int i=0;i<rows;i++)
{
for(int j=0;j<cols;j++)
{
mat_[i][j]=defaultValue;
}
}
}
int Matrix::rows() const
{
return rows_;
}
int Matrix::cols() const
{
return cols_;
}
Matrix::Indexer::Indexer(float* arr,int cols):
arr_(arr),
cols_(cols)
{}
Matrix::Indexer Matrix::operator[](int index)
{
if(index>=rows_)
throw "Out of row index";
return Indexer(mat_[index],cols_);
}
float& Matrix::Indexer::operator[](int index)
{
if(index>=cols_)
throw "Out of cols index";
return arr_[index];
}
Matrix Matrix::operator+(const Matrix& other)//error
{
if(other.rows()!=this->rows()||other.cols()!=this->cols())
throw "rows and cols are not equal";
Matrix result(other.rows(),other.cols());
for(int i=0;i<rows();i++)
{
for(int j=0;j<cols();j++)
{
result[i][j]=mat_[i][j]+other[i][j];//error: passing 'const Matrix' as 'this' argument of 'Matrix::Indexer Matrix::operator' discards qualifiers [-fpermissive
}
}
return result;
}
答案 0 :(得分:2)
你的operator+
应该是一个const成员函数,因为它不会修改它所调用的对象。对于其他人,您可能还需要ConstIndexer
和operator[]( int index ) const
。
与您的问题完全无关,但是:您可能最好使用std::vector<float>
作为实际数据。它既易于使用,而且(可能)更快。
答案 1 :(得分:0)
我通过添加
解决了我的问题const Indexer operator[](int index)const;
和
const float& operator[](int index)const;
我的代码:
<强> Matrix.h 强>
#ifndef MATRIX_H
#define MATRIX_H
#include <iostream>
class Matrix
{
public:
Matrix(const int rows, const int cols, float defaultValue=0);
//TODO copy constructor , destructor
int rows() const;
int cols() const;
Matrix operator+(const Matrix& other) const;
class Indexer
{
public:
Indexer(float* arr,int cols);
const float& operator[](int index)const;
float& operator[](int index);
private:
float* arr_;
int cols_;
};
Indexer operator[](int index);
const Indexer operator[](int index)const;
friend std::ostream& operator<<(std::ostream& os,const Matrix& m);
private:
int rows_;
int cols_;
float **mat_;
};
#endif // MATRIX_H
<强> Matrix.cpp 强>
#include "matrix.h"
Matrix::Matrix(const int rows, const int cols, float defaultValue) :
rows_(rows),
cols_(cols)
{
mat_=new float*[rows_];
for(int i=0;i<rows;i++)
{
mat_[i]=new float[cols];
}
for(int i=0;i<rows;i++)
{
for(int j=0;j<cols;j++)
{
mat_[i][j]=defaultValue;
}
}
}
int Matrix::rows() const
{
return rows_;
}
int Matrix::cols() const
{
return cols_;
}
Matrix::Indexer::Indexer(float* arr,int cols):
arr_(arr),
cols_(cols)
{}
const float &Matrix::Indexer::operator[](int index) const
{
if(index>=cols_)
throw "Out of cols index";
return arr_[index];
}
Matrix::Indexer Matrix::operator[](int index)
{
if(index>=rows_)
throw "Out of row index";
return Indexer(mat_[index],cols_);
}
const Matrix::Indexer Matrix::operator[](int index) const
{
if(index>=rows_)
throw "Out of row index";
return Indexer(mat_[index],cols_);
}
std::ostream& operator<<(std::ostream &os, const Matrix &m)
{
for(int i=0;i<m.rows();i++)
{
for(int j=0;j<m.cols();j++)
{
os<<m[i][j]<<" ";
}
os<<"\n";
}
return os;
}
float& Matrix::Indexer::operator[](int index)
{
if(index>=cols_)
throw "Out of cols index";
return arr_[index];
}
Matrix Matrix::operator+(const Matrix& other) const
{
if(other.rows()!=this->rows()||other.cols()!=this->cols())
throw "rows and cols are not equal";
Matrix result(other.rows(),other.cols());
for(int i=0;i<rows();i++)
{
for(int j=0;j<cols();j++)
{
result[i][j]=mat_[i][j]+other[i][j];
}
}
return result;
}