Overloaded functions: Compile OK on MSVC++ but not on g++

U

user

I am trying to port a simple vector/matrix class library to mingw
(v3.4.2). I have used this library with no problem on MSVC++ v6 for a
number of years. My difficulty is that it does not compile under g++!
Two issues:

Firstly, if I define x to be a CColumnVector, y to be a CRowVector and M
to be a CMatrix (of the right size!) then:

M = x * y;

works fine with both compilers where:

CMatrix operator *(CColumnVector& a, CRowVector& b);

But for:

M = Transpose(y) * y;

where CColumnVector Transpose(CRowVector& a);

g++ gives me "error: no match for 'opertator*' in
Transpose(CRowVector&)() * y" followed by a list of 11 candidates
including the correct function. MSVC++ has no problem with this and can
correctly interpret the context of the CColumnVector returned by the
Transpose function. g++ it appears, cannot!


Second, if I have:

A = B * C * D;

where A..D are matrices and where:

CMatrix operator *(CMatrix& A, CMatrix& B);

g++ gags with

"error: no match for 'opertator*' in 'operator*(CMatrix&,CMatrix&)
(((CMatrix&) (&C))) * D'"

and again, presents a long list of possibilities including the correct
function. MSVC++ sails through no problem.

In fact, it seems g++ is not happy about multiplying three matrices (or
more) although it's happy with two! Which is a rather constrained
version of overloading...

Anybody any ideas on solving either/both of these problems?

Cheers,

Peter
 
R

Rolf Magnus

I am trying to port a simple vector/matrix class library to mingw
(v3.4.2). I have used this library with no problem on MSVC++ v6 for a
number of years. My difficulty is that it does not compile under g++!
Two issues:

Firstly, if I define x to be a CColumnVector, y to be a CRowVector and M
to be a CMatrix (of the right size!) then:

M = x * y;

works fine with both compilers where:

CMatrix operator *(CColumnVector& a, CRowVector& b);

Do you modify your vectors? If not, why aren't they const?
But for:

M = Transpose(y) * y;

where CColumnVector Transpose(CRowVector& a);

g++ gives me "error: no match for 'opertator*' in
Transpose(CRowVector&)() * y" followed by a list of 11 candidates
including the correct function.

That's because the return value of Transpose() is a temporary, which cannot
be bound to a non-const reference.
MSVC++ has no problem with this and can correctly interpret the context of
the CColumnVector returned by the Transpose function.

No, it doesn't correctly interpret it. Generating an error message is the
correct way.
g++ it appears, cannot!

g++ is right.
Second, if I have:

A = B * C * D;

where A..D are matrices and where:

CMatrix operator *(CMatrix& A, CMatrix& B);

g++ gags with

"error: no match for 'opertator*' in 'operator*(CMatrix&,CMatrix&)
(((CMatrix&) (&C))) * D'"

and again, presents a long list of possibilities including the correct
function. MSVC++ sails through no problem.

Same problem. The return value of operator* is a temporary, which cannot be
bound to the non-const reference in the other call to operator*.
In fact, it seems g++ is not happy about multiplying three matrices (or
more) although it's happy with two! Which is a rather constrained
version of overloading...

Anybody any ideas on solving either/both of these problems?

Read up on const-correct programming.
 
G

Greg

I am trying to port a simple vector/matrix class library to mingw
(v3.4.2). I have used this library with no problem on MSVC++ v6 for a
number of years. My difficulty is that it does not compile under g++!
Two issues:

Firstly, if I define x to be a CColumnVector, y to be a CRowVector and M
to be a CMatrix (of the right size!) then:

M = x * y;

works fine with both compilers where:

CMatrix operator *(CColumnVector& a, CRowVector& b);

This declaration should be:

CMatrix operator*(const CColumnVector& a, const CRowVector& b)

The values being multiplied should not be changed by the multiplication
(I would hope). The "const" qualifier allows the compiler to pass
temporaries for a or b (that is, intermediate results from other
operations or function results) to this overloaded multiply operator.
But for:

M = Transpose(y) * y;

where CColumnVector Transpose(CRowVector& a);

g++ gives me "error: no match for 'opertator*' in
Transpose(CRowVector&)() * y" followed by a list of 11 candidates
including the correct function. MSVC++ has no problem with this and can
correctly interpret the context of the CColumnVector returned by the
Transpose function. g++ it appears, cannot!


Second, if I have:

A = B * C * D;

where A..D are matrices and where:

CMatrix operator *(CMatrix& A, CMatrix& B);

Same as above, make the parameters const references.

Greg
 
U

user

Thanks for the prompt reply.

So if I change the function declarations to use const arguments, it
should compile? (This may not be straightforward because of what I am
doing within the classes but I'll have a look.)

"...the return value of Transpose() is a temporary, which cannot be
bound to a non-const reference".

OK. Any suggestions for a tractable source on such information that will
explain this arcane point to me? Similarly, anything accessible on
const-correct programming?


Peter
 
D

deane_gavin

<top-posting rearranged>


<snip discussion of code containing two examples of trying to bind a
temporary to a non-const reference>
Thanks for the prompt reply.

So if I change the function declarations to use const arguments, it
should compile? (This may not be straightforward because of what I am
doing within the classes but I'll have a look.)

"...the return value of Transpose() is a temporary, which cannot be
bound to a non-const reference".

OK. Any suggestions for a tractable source on such information that will
explain this arcane point to me? Similarly, anything accessible on
const-correct programming?

The FAQ is probably a good place to start. It has a whole section on
const correctness

http://www.parashift.com/c++-faq-lite/

Gavin Deane
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top