TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: ALBERTO MONTEIRO
from: THOMAS MAEDER
date: 1997-12-29 19:35:00
subject: What`s wrong with this?

AM> template  class Module {
AM> public:
AM>    Ring v[dimension];
AM>    Module();
AM>    Module operator+(const Module m2) const;
AM>    friend Module operator*(Ring x, const Module m2);
AM> };
AM> Module::Module()        // here the compiler say it's wrong
You have to write
template     
Module::Module()
AM> Module Module::operator+(const Module m2) const
AM> {
AM>         Module rval;
AM>         for (int i = 0; i < dimension; i++)
AM>                 rval.v[i] = v[i] + m2.v[i];
AM>         return rval;
AM> }
To have the module instances look more like built-in types, you  might
also  define  an  operator+=  and  implement  operator+  in  terms  of
operator+=.
AM> Module operator*(Ring x, const Module m2)
AM> {
AM>         Module rval;
AM>         for (int i = 0; i < dimension; i++)
AM>                 rval.v[i] = x * m2.v[i];
AM>         return rval;
AM> }
This operator is not symmetrical. You might also define
template     
Module 
Module::operator*(const Module &m, Ring x)
and implement both of them in terms of an operator*=.
Note that I pass  the  module  by  reference  which  is  usually  more
efficient.
Thomas
---
 þ MM 1.0 þ Unregistered þ MailMaker - Your Windows offline reader!
---------------
* Origin: McMeier & Son BBS (2:301/138)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.