public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Operator overloading problem
@ 2002-10-28 11:14 Aïcha & Didier Rizzotti
  2002-10-28 12:00 ` Andrea "Fyre Wyzard" Bocci
  0 siblings, 1 reply; 2+ messages in thread
From: Aïcha & Didier Rizzotti @ 2002-10-28 11:14 UTC (permalink / raw)
  To: gcc

Hello,

I've written a small vector2 class with some operator overloading:

--------------------------------------------------------------

#include "vector2.h"

vector2::vector2(int dimension) : vector(dimension)     //Constructeur
{
}

vector2::vector2(vector2 &v) : vector(v.dim)    //Constructeur de
recopie
{
        int i;

        for(i=0;i<dim;i++)
                put(v.get(i),i);
}

void vector2::operator =(vector2 &v)//assignation
{//vecteur de mêmes taille uniquement
        int i;

        for(i=0;i<dim;i++)
                put(v.get(i),i);
}

vector2 vector2::operator +(vector2 &v)
{
        vector2 tmp(dim);

        tmp.add(*this);
        tmp.add(v);
        
        return tmp;
}

vector2 vector2::operator -(vector2 &v)
{
        vector2 tmp(dim);

        tmp.add(*this);
        tmp.sub(v);
        
        return tmp;
}

float vector2::operator *(vector2 &v)
{
        return prod(v);
}

---------------------------------------------------------------

And I use it in a small sample programm:

---------------------------------------------------------------

#include <iostream>
#include "vector2.h"
using namespace std;

#define DIM 4

int main()
{
        vector2 v1(DIM);//instanciation de 3 vecteurs
        vector2 v2(DIM);
        vector2 v3(DIM);
        int i;
        float k=0.0;
        
        for (i=0;i<DIM;i++)//remplissage des vecteurs
                {
                        v1.put(k,i);
                        v2.put(k+1,i);
                        k += 1;
                }

        cout<<"Vecteur v1: ";
        for (i=0;i<DIM;i++)//affichage des vecteurs
        {
                cout<<v1.get(i)<<" ";
        }
        cout<<"\nVecteur v2: ";
        for (i=0;i<DIM;i++)
        {
                cout<<v2.get(i)<<" ";
        }
        cout<<"\n";

        v3 = v1 + v2; //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< BUG
        //v3.operator = (v1 + v2);

        cout<<"\nAprès addition: ";//affichage du résultat
        for (i=0;i<DIM;i++)
        {
                cout<<v3.get(i)<<" ";
        }
        cout<<"\n";

        cout<<"Produit scalaire: "<< v1 * v2 <<"\n\n";

        return 0;
 }

---------------------------------------------------------------

The programm work well with Visual C++ 6.0, but return the following
error with g++:

D:\Perso\Didier_perso\Job\EPFL\ex\matrice_vect>g++ ex_vector2.cpp
vector2.cpp ve
ctor.cpp matrix.cpp
ex_vector2.cpp: In function `int main()':
ex_vector2.cpp:45: no matching function for call to
`vector2::operator=(vector2
   )'
vector2.h:17: candidates are: void vector2::operator=(vector2&)


I can not see where is the problem (maybe temporary object).
Can-you help me ?

Didier Rizzotti

--------------------------------------------------------------
Didier Rizzotti
Suchet 1
1070 Puidoux (Switzerland)
tél:+41.79.476.21.37
mailto:rizzotti@freesurf.ch

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Operator overloading problem
  2002-10-28 11:14 Operator overloading problem Aïcha & Didier Rizzotti
@ 2002-10-28 12:00 ` Andrea "Fyre Wyzard" Bocci
  0 siblings, 0 replies; 2+ messages in thread
From: Andrea "Fyre Wyzard" Bocci @ 2002-10-28 12:00 UTC (permalink / raw)
  To: Aïcha & Didier Rizzotti; +Cc: gcc

Not sure (you didn't post vector2.h :-, so I couldn't try it) but - you can 
try to declare

vector2::vector2(vector2 &v) : vector(v.dim)    //Constructeur de recopie

as

vector2::vector2(const vector2 &v) : vector(v.dim)    //Constructeur de recopie

and see if that solve the issue.

HTH,
fwyzard

(PS. Send questions like this to gcc-help@..., not gcc@..., thy are more 
likey to get answers :-)


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2002-10-28 17:12 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-10-28 11:14 Operator overloading problem Aïcha & Didier Rizzotti
2002-10-28 12:00 ` Andrea "Fyre Wyzard" Bocci

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).