This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Operator overloading problem


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]