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

Discussion in 'C++' started by user@domain.invalid, Oct 21, 2005.

  1. Guest

    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, 2005
    #1
    1. Advertising

  2. Rolf Magnus Guest

    lid 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.
    Rolf Magnus, Oct 21, 2005
    #2
    1. Advertising

  3. Greg Guest

    lid 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
    Greg, Oct 21, 2005
    #3
  4. Guest

    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:
    > lid 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, 2005
    #4
  5. Guest

    <top-posting rearranged>

    lid wrote:
    > Rolf Magnus wrote:
    > > lid 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, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jef Driesen
    Replies:
    1
    Views:
    471
    Michael Mellor
    Jan 29, 2004
  2. =?ISO-8859-1?Q?Christian_Engstr=F6m?=

    base/derived ambiguity with gcc, but not with MSVC

    =?ISO-8859-1?Q?Christian_Engstr=F6m?=, Feb 11, 2004, in forum: C++
    Replies:
    2
    Views:
    358
    =?ISO-8859-1?Q?Christian_Engstr=F6m?=
    Feb 12, 2004
  3. =?ISO-8859-1?Q?Christian_Engstr=F6m?=

    Another base/derived problem with gcc, but not with MSVC

    =?ISO-8859-1?Q?Christian_Engstr=F6m?=, Feb 12, 2004, in forum: C++
    Replies:
    7
    Views:
    357
    John Harrison
    Feb 13, 2004
  4. Pierre-Marc Fournier

    Template code works with g++ but not MSVC++

    Pierre-Marc Fournier, Jul 13, 2004, in forum: C++
    Replies:
    6
    Views:
    460
    Alf P. Steinbach
    Jul 13, 2004
  5. raylopez99
    Replies:
    2
    Views:
    380
    raylopez99
    Jun 13, 2006
Loading...

Share This Page