aaamourao
aaamourao

Reputation: 128

Encapsulate Boost ublas matrix operator ()

Considering the following class:

namespace ublas = boost::numeric::ublas;
class Foo {
private:
    ublas::compressed_matrix<float> foo_mtr_;
}

I want to encapsulate operator() in such a way that

Foo foo;
foo(2, 3) = 3.0f;

would be equivalent to

foo_mtr_(2, 3) = 3.0f;

I've tried

float &operator () (int i, int_type j) {
    return foo_mtr_(i, j);
}

But it does not compile:

error: binding reference of type ‘float&’ to ‘const float’ discards qualifiers

Upvotes: 0

Views: 49

Answers (1)

rafix07
rafix07

Reputation: 20938

ublas::compressed_matrix<T> has defined types reference and const_reference, you should use them instead T& or const T&. Rewrite your operator()(int,int) as follows

ublas::compressed_matrix<float>::reference operator () (int i, int_type j) {
    return foo_mtr_(i, j);
}

Upvotes: 1

Related Questions