Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.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++11_Assign_Rvalue - Fatal编程技术网

C++ 如何在方法中同时使用右值输入(运算符=)和普通参数?

C++ 如何在方法中同时使用右值输入(运算符=)和普通参数?,c++,c++11,assign,rvalue,C++,C++11,Assign,Rvalue,我试图在这里创建一个类方法,它能够将运算符为的右值赋值给数组,这也需要输入索引。目前,代码如下所示: #include <iostream> using namespace std; class Matrix { public: int nz,nx; float *elements; int size() {return nz * nx;} // Return size of the matrix f

我试图在这里创建一个类方法,它能够将运算符为的右值赋值给数组,这也需要输入索引。目前,代码如下所示:

#include <iostream>
    
using namespace std;

class Matrix
{
    public:
        int nz,nx;
        float *elements;
        int size() {return nz * nx;} // Return size of the matrix
        float idx(int,int);
        void idxAssign(int,int) operator = (float);
        void allocate() {elements = new float[size()];}
};

void Matrix::idxAssign (int i,int j) operator = (float &rvalue)
{
    elements[j * nz + i] = rvalue;
}


float Matrix::idx (int i, int j)
{
    // Return a element of the matrix from the index
    return elements[j * nz + i];
}

void dotprodMatrix(Matrix A, Matrix B)
{
    if(A.size() == B.size()){
        for(int i=0; i<A.size; i++){
            A.idxAssign(i,j) = A.idx(i,j) + B.idx(i,j);
        }
    }
}

int main()
{
    Matrix A;
    A.nz = 32;
    A.nx = 32;

    Matrix B;
    B.nz = 32;
    B.nx = 32;

    // Method to allocate both matrices in the cpu
    A.allocate();
    B.allocate();

    // Fill up
    for(int i=0; i<B.size(); i++)
    {
        A.elements[i] = 2.0;
        B.elements[i] = 5.0;
    }
    dotprodMatrix(A, B);
    // Print results
    for(int i=0; i<A.nz; i++)
    {
        for(int j=0; j<A.nx; j++)
        {
            cout<<A.idx(i,j)<<"  ";
    }
        cout<<endl;
    }


    delete A.elements;
    delete B.elements;
    return 0;
}
在执行时,编译器说at声明它期望一个;对空的IDxAdvutint,int,我对C++类、运算符和什么都不太了解,所以请原谅我的代码质量。我花了很多时间试图寻找解决方案,直到我最终决定在这里寻求帮助。谢谢你能帮我一点忙

提前谢谢

<空格IDXPoint,int运算符=浮点不是有效的C++。void idxAssignint,int是一个函数声明,operator=float是另一个格式错误的函数运算符重载声明

可能你要找的是float&idxAssignint,int之类的东西

浮点和矩阵::idxAssign int i,int j { 返回元素[j*nz+i]; } 现在您可以使用此语法赋值

基质垫; ... mat.idxAssign0,0=10; 这里有一个非常量getter。因为我们实际上没有分配任何东西,我们可以重命名它

浮点和矩阵::getint i,int j { 返回元素[j*nz+i]; }
现在,对于阅读本文的人来说,API更加清晰了。通常,使用函数的赋值是使用一些语法完成的,比如:void Matrix::Assignint i,int j,float value;但是使用getter返回引用,然后分配它是非常常见的。

您认为应该如何使用void Matrix::idxAssign int i,int j operator=float&rvalue?请注意,它是一种格式不正确的C++语言,您应该阅读它的相关内容。要么你为了简洁而省略了,要么你的矩阵有点broken@largest_prime_is_463035818,我在面向对象编程方面是个乞丐,我一定会读你提供的文章。非常感谢。在这一点上,我建议更改名称,因为idxAssign是一个getter,不做任何赋值。但是,通过编写返回引用的非常量getter来启用赋值是一种非常常见的使用模式,值得在这里介绍。非常感谢!这是如此简单,但不知何故,我可能缺乏有关指针的知识。不管怎样,谢谢你的快速回复。@NathanPierson,我一得到答案就这么做了,谢谢。很高兴知道我的问题对外面的人很有用@内森·皮尔森:我把它加到了答案上。谢谢你的建议!