How to have both rvalue input (with operator =) and normal arguments in a method?

  assign, c++, c++11, rvalue

I kinda trying to make a class method here that is able to assign a rvalue with "operator =" to an array, which requires also the input of the indexes. For now the code looks like this:

#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;
}

While executing, the compiler says that at declares that it expected a ";" right after void idxAssign(int,int). I am not very knowledgeable about c++ classes, operators and what not, so please forgive the quality of my code. I spent a lot of hours trying to look for a solution until I finally decided to ask for help here. So thanks if you can help me a little bit!

Thx in advance!

Source: Windows Questions C++

LEAVE A COMMENT