473,396 Members | 2,092 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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
Oct 21 '05 #1
4 2081
us**@domain.invalid wrote:
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.

Oct 21 '05 #2
us**@domain.invalid wrote:
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

Oct 21 '05 #3
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

Rolf Magnus wrote:
us**@domain.invalid wrote:

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.

Oct 21 '05 #4
<top-posting rearranged>

us**@domain.invalid wrote:
Rolf Magnus wrote:
us**@domain.invalid wrote:
<snip discussion of code containing two examples of trying to bind a
temporary to a non-const reference>
Anybody any ideas on solving either/both of these problems?

Read up on const-correct programming.

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

Oct 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Nitin Bhardwaj | last post by:
Thanx in advance for the response... I wanna enquire ( as it is asked many a times in Interviews that i face as an Engg PostGraduate ) about the overloading capability of the C++ Language. ...
4
by: frankg | last post by:
When I compile this file ------------------------------------------------------ #include <stdarg.h> template <class Any> void var_arg_func(int dimension_count, ...) { int dimensions;...
1
by: Jef Driesen | last post by:
I have 4 overloaded functions 'deallocate': template <typename T> void deallocate(T* mem); template <typename T> void deallocate(T** mem); template <typename T> void deallocate(T*** mem);...
4
by: DaKoadMunky | last post by:
My question relates to storing the addresses of functions generated by templates in pointers to functions. <CODE> template<class T> void Global() {} namespace ANamespace {
4
by: George Economos | last post by:
Hi all, I am using msvc 7.1 and have encountered the following code: ----------- A.hpp ----------- class A {
44
by: bahadir.balban | last post by:
Hi, What's the best way to implement an overloaded function in C? For instance if you want to have generic print function for various structures, my implementation would be with a case...
2
by: raylopez99 | last post by:
I'm having problems compiling complex reference declarations in MSVC++.NET 2002 IDE. Here is an example: // --Foo.h-- #include "Bar.h" class Bar; //forward decl. to a class Bar in...
2
by: desktop | last post by:
If a function should work with different types you normally overload it: void myfun(int a){ // do int stuff } void myfun(std::string str){ // do string stuff }
2
by: BruceWho | last post by:
I downloaded boost1.35.0 and built it with following command: bjam --toolset=msvc-7.1 --variant=release --threading=multi -- link=shared --with-system stage and it failed to compile, error...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.