delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/08/05/13:35:35

Newsgroups: comp.os.msdos.djgpp
From: Peter Berdeklis <peter AT atmosp DOT physics DOT utoronto DOT ca>
Subject: Re: C++ class interaction and overloading (should be simple answer)
Message-ID: <Pine.SGI.3.91.970805112330.4099A-100000@atmosp.physics.utoronto.ca>
Nntp-Posting-Host: chinook.physics.utoronto.ca
Sender: news AT info DOT physics DOT utoronto DOT ca (System Administrator)
Mime-Version: 1.0
Organization: University of Toronto - Dept. of Physics
In-Reply-To: <5s3l9f$o46@freenet-news.carleton.ca>
Date: Tue, 5 Aug 1997 15:27:07 GMT
References: <33E3B107 DOT 63DEA07F AT execulink DOT com> <5s3l9f$o46 AT freenet-news DOT carleton DOT ca>
Lines: 46
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

On 4 Aug 1997, Paul Derbyshire wrote:

> 
> Jeff Weeks (pweeks AT execulink DOT com) writes:
> > Hi there...
> > 
> > I've got a C++ problem here.  I've defined a two classes: Vector and
> > Matrix.  I'd like to be able to do this:
> > 
> > Vector2 = Vector1 * Matrix1
> > 
> > But I can't get that to work.  Is this possible?
> > 
> > What I tried was this (in my Vector class):
> > 
> > class Vector {
> >   ...
> >   public:
> >     inline friend Vector operator * (Matrix& mat, Vector& vect);
> 
> Try omitting the space after "operator":
> 
>      inline friend Vector operator* (Matrix& mat, Vector& vect);
>                                  ^^

Not quite Paul.  As Eric said in another followup, this is perfectly legal.

The problem is with the order of your arguments.  You have defined 

Matrix * Vector

but are asking for

Vector * Matrix

Unless there is a defined cast from Matrix to Vector and from Vector to 
Matrix, the compiler won't be able to find a match, because switching the 
order of the arguments is illegal.

Either always call Vector * Matrix, or define both.  The one could be 
just an inline which calls the other with the arguments reversed.

---------------
Peter Berdeklis
Dept. of Physics, Univ. of Toronto

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019