-1

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!

Mansoor
  • 2,357
  • 1
  • 17
  • 27
Átila
  • 1
  • 1
    How do you imagine `void Matrix::idxAssign (int i,int j) operator = (float &rvalue)` should be used? (note that as it is, it's ill-formed C++) – scohe001 Feb 17 '21 at 14:17
  • you should read about the [rule of 3/5](https://en.cppreference.com/w/cpp/language/rule_of_three). Either you left it out for brevity or your `Matrix` is kinda broken – 463035818_is_not_an_ai Feb 17 '21 at 14:58
  • @largest_prime_is_463035818, I am very much a begginer in terms of object oriented programming, I will surely read the article you provided. Thank you! – Átila Feb 17 '21 at 19:05

1 Answers1

2

void idxAssign(int,int) operator = (float) is not valid C++. void idxAssign(int,int) is one function declaration and operator = (float) is another ill-formed function (operator overload) declaration.

Possibly what your looking for is something like float& idxAssign(int, int).

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

Now you can assign values using this syntax,

Matrix mat;
...

mat.idxAssign(0, 0) = 10;

What you have here is a non-const getter. And since we aren't actually assigning anything, we can rename it,

float& Matrix::get(int i, int j)
{
    return elements[j * nz + i];
}

Now the API is much more clear for someone whos reading this. Usually assignments using functions are done using some syntax like: void Matrix::Assign(int i, int j, float value); but using getters to return a reference and then assigning it is pretty common.

D-RAJ
  • 3,263
  • 2
  • 6
  • 24
  • 2
    At this point I would suggest a name change, though, since `idxAssign` is a getter and doesn't do any assignment. But enabling assignment by writing non-`const` getters that return references is a pretty common usage pattern and worth describing here. – Nathan Pierson Feb 17 '21 at 14:29
  • 1
    Thank you so much! It is so simple but somehow I probably lacking knowledge about pointers. Either way, thank you for the quick response. – Átila Feb 17 '21 at 14:30
  • @NathanPierson, I did that as soon I've got the answer, thank you. Good to know that my question will be useful to someone out there! – Átila Feb 17 '21 at 14:31
  • @NathanPierson I added it to the answer. Thank you for the suggestion! – D-RAJ Feb 17 '21 at 14:37