(x,y,z) Operator overloading on matrix

J

Jaco Naude

Hi

I'm building a 3D matrix and I've overloaded the () operators as
follow:

//! Subscript operator overload.
complex<double> operator() (unsigned x, unsigned y, unsigned z);
//! Subscript operator const overload.
complex<double> operator() (unsigned x, unsigned y, unsigned z) const;

In these functions I return the value at the given position. Reading
the values works fine, but assigning them does not.

For example, the following does not work:

Matrix matrix1(10,10,10);
complex<double> value(2,2);
matrix1(1,1,1) = value;

I followed the following topic in the C++ FAQ: [13.10] How do I create
a subscript operator for a Matrix class?
http://www.parashift.com/c++-faq-lite/operator-overloading.html#faq-13.10

At the end of the topic they say that the following should be
possible:

Matrix m(10,10);
m(5,8) = 106.15;

I don't see what they did different than my implementation and I don't
understand why it should be possible with just the two overloaded
operators.

Any insights will be appreciated.
Thanks,
Jaco
 
J

Jaco Naude

Hi

I'm building a 3D matrix and I've overloaded the () operators as
follow:

//! Subscript operator overload.
complex<double> operator() (unsigned x, unsigned y, unsigned z);
//! Subscript operator const overload.
complex<double> operator() (unsigned x, unsigned y, unsigned z) const;

In these functions I return the value at the given position. Reading
the values works fine, but assigning them does not.

For example, the following does not work:

Matrix matrix1(10,10,10);
complex<double> value(2,2);
matrix1(1,1,1) = value;

I followed the following topic in the C++ FAQ: [13.10] How do I create
a subscript operator for a Matrix class?http://www.parashift.com/c++-faq-lite/operator-overloading.html#faq-1...

At the end of the topic they say that the following should be
possible:

Matrix m(10,10);
m(5,8) = 106.15;

I don't see what they did different than my implementation and I don't
understand why it should be possible with just the two overloaded
operators.

Any insights will be appreciated.
Thanks,
Jaco

My bad, apologies. I missed the & sign in front of the non-const
overload.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,815
Latest member
treekmostly22

Latest Threads

Top