无法将const值发送给操作员 使用操作员[] []

can not send const value to operator+ that use operator[][]

本文关键字:操作员 const      更新时间:2023-10-16

我想实现使用operator [] []。

的矩阵类

我写了以下代码,但问题是我不能将 const 值发送给操作员 !!(错误:传递'const矩阵'as as this'''m artrix :: indexer matrix:this'参数::操作员丢弃预选赛[-fpermissive])

如果将其更改为矩阵操作员 (矩阵& other);它可以正常工作...

我认为我需要两个矩阵:: 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;
 }

您的 operator+应该是const成员函数,因为它不会修改调用的对象。其余的,您可能还需要ConstIndexeroperator[]( int index ) const

与您的问题完全无关,但是:您可能会使用std::vector<float>来获取实际数据。既容易使用,又(可能)更快。

我通过添加

解决了我的问题
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;
 }