Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何声明可以执行赋值操作的函数?(C+;+;)_C++_Function_Assignment Operator - Fatal编程技术网

C++ 如何声明可以执行赋值操作的函数?(C+;+;)

C++ 如何声明可以执行赋值操作的函数?(C+;+;),c++,function,assignment-operator,C++,Function,Assignment Operator,我正在尝试创建一个类似于std::vector中的at()函数的函数。我知道如何为=重载操作符,但这不是我想要的。我有一个矩阵对象,我希望沿着覆盖矩阵列向量的路线执行一个操作,即 int rowNumber = 3; int columnNumber = 3; Matrix myMatrix(rowNumber, columnNumber); Vector myColumnVector(rowNumber); myMatrix.col(2) = myColumnVector; 其中col()是

我正在尝试创建一个类似于
std::vector
中的
at()
函数的函数。我知道如何为
=
重载操作符,但这不是我想要的。我有一个矩阵对象,我希望沿着覆盖矩阵列向量的路线执行一个操作,即

int rowNumber = 3; int columnNumber = 3;
Matrix myMatrix(rowNumber, columnNumber);
Vector myColumnVector(rowNumber);
myMatrix.col(2) = myColumnVector;
其中
col()
是赋值函数。如何声明此函数?

col()
不是赋值函数

operator=()
是赋值函数


col()。在这种情况下,引用
向量
(即
向量&
)将完成此工作。

您可以使用一些代理:

struct Matrix;

struct ColWrapper
{
    Matrix* mMatrix;
    int mIndex;

    ColWrapper& operator =(const std::vector<double>& d);
};

struct RowWrapper
{
    Matrix* mMatrix;
    int mIndex;

    RowWrapper& operator =(const std::vector<double>& d);
};

struct Matrix
{
    std::vector<double> mData;
    int mRow;

    Matrix(int row, int column) : mData(row * colunmn), mRow(row) {}


    ColWrapper col(int index) { return {this, index}; }
    RowWrapper row(int index) { return {this, index}; }
};

ColWrapper& ColWrapper::operator =(const std::vector<double>& ds)
{
    auto index = mIndex * mMatrix->mRow;

    for (auto d : ds) {
        mMatrix->mData[index] = d;
        index += 1;
    }
    return *this;
}

RowWrapper& RowWrapper::operator =(const std::vector<double>& ds)
{
    auto index = mIndex;

    for (auto d : ds) {
        mMatrix->mData[index] = d;
        index += mMatrix->mRow;
    }
    return *this;
}

struct矩阵;
结构ColWrapper
{
矩阵*mMatrix;
int mIndex;
ColWrapper&运算符=(const std::vector&d);
};
结构行包装器
{
矩阵*mMatrix;
int mIndex;
行包装器和运算符=(const std::vector&d);
};
结构矩阵
{
std::向量mData;
int mRow;
矩阵(int行,int列):mData(row*colunmn),mRow(row){
ColWrapper col(int index){return{this,index};}
行包装器行(int索引){return{this,index};}
};
ColWrapper和ColWrapper::operator=(const std::vector和ds)
{
自动索引=mIndex*mMatrix->mRow;
用于(自动d:ds){
mMatrix->mData[索引]=d;
指数+=1;
}
归还*这个;
}
RowWrapper&RowWrapper::operator=(const std::vector&ds)
{
自动索引=mIndex;
用于(自动d:ds){
mMatrix->mData[索引]=d;
索引+=mMatrix->mRow;
}
归还*这个;
}
at()
返回一个引用。你熟悉推荐人吗?让函数返回一个引用。可能是一个带有
操作符=
重载的助手对象。如果你不熟悉这些概念,我必须把你直接指向C++的书。这两个都是非常广泛的主题,StActOfFult.com实际上不是设置为一个定制的C++教程站点。有一个<代码>矩阵::COR()/<代码>函数,返回对基础数据类型的引用。添加更多关于如何准确定义
矩阵
类的信息,以便在此处获得一些简明的答案。