& at?

So for a project we have we have to be able to use the "at" function to return a element for a Matrix. I get how to find the element in the Matrix, I just need some help with the syntax.

In our header file the line that defines the at function is:
 
double & at(uint row, uint col);


How would I define this in my .cpp file? Right now I have:
 
  double Matrix::& at(uint row, uint col){}


I'm pretty sure that's wrong though.

As for what I'm going to do in the function, I should be able to call my copy constructor and then just return the elements in array[row][col]
You have the & in the wrong place in your definition (.cpp).

1
2
3
double & Matrix::at(uint row, uint col)
{  return arr[row][col];
}


Since you're returning by reference, there is no need for a copy constructor.

Last edited on
I implemented it exactly that way but I keep getting a weird error "undefined reference to `Matrix::at(unsigned int, unsigned int) const' " I have no idea why
So I fixed that, I had to add a function:
 
const double & Matrix::at (uint row, uint col) cons


It does the same thing. However, now that I get it working and try implementing it, whenever I create more than 1 Matrix object it nowgives me a segmentation error
Topic archived. No new replies allowed.