probleme pour créer un operator%
Bonjour je debute en C++ et là depuis deux jours je traine sur une classe vecteur car quand je lance la compilation il me renvoit l'erreur ci-dessous
g++ -c vecteur.cpp main.cpp
Code:
1 2 3 4
|
**main.cpp: In function int main(int, char**):
main.cpp:12: error: no match for operator= in V4 = V1.vecteur::operator%(((const vecteur&)((const vecteur*)(& V2))))
vecteur.h:17: note: candidates are: vecteur& vecteur::operator=(const vecteur&)** |
ci dessous mes trois fichiers ""main.cpp; vecteur.cpp; vecteur.h"
main.cpp
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| #include"vecteur.h"
#include<iostream>
#include<string>
using namespace std;
int main(int argc, char *argv[]){
vecteur V1(14,12,8);
vecteur V2(-15,4,25);
vecteur V3,V4,V5;
V3 = V1+V2;
V4 = V1 % V2;
V5 = V1^V2;
cout <<"( "<<V1<<" ) + ( "<<V2<< ") ( "<<V3<< " )"<< endl;
cout <<"( "<<V1<<" ) % ( "<<V2<< ") ( "<<V4<< " )"<< endl;
cout <<"( "<<V1<<" ) ^ ( "<<V2<< ") ( "<<V5<< " )"<< endl;
return 0;
} |
vecteur.cpp
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
| #include"vecteur.h"
#include<iostream>
#include<string>
using namespace std;
//static nb_pts=0;
vecteur::vecteur(double a, double b, double c){
x=a;y=b;z=c;
//nb_pts++
}
vecteur& vecteur::operator=(vecteur const& v){
this->x=v.x;
this->y=v.y;
this->z=v.z;
return *this;
}
vecteur::vecteur(vecteur const& v){
this->x=v.x;
this->y=v.y;
this->z=v.z;
}
vecteur::~vecteur(){
//nb_pt--;
}
vecteur vecteur::operator+(vecteur const& v){
vecteur u;
u.x=v.x+this->x;
u.y=v.y+this->y;
u.z=v.z+this->z;
return u;
}
vecteur& vecteur::operator+=(vecteur const& v){
this->x+=v.x;//(*this)+=*this+v;
this->y+=v.y;
this->z+=v.z;
return *this;
}
bool vecteur::operator==(vecteur const& v){
return (this->x==v.x && this->y==v.y && this->z==v.z);
}
double vecteur::operator%(vecteur const& v){
//double res;
return (this->x*v.x+this->y*v.y+this->z*v.z);
//return res;
}
vecteur& vecteur::operator^(vecteur const& v){
this->x=(this->y*v.z)-(this->z*v.y);
this->y=(-this->x*v.z)+(v.x*this->z);
this->z=(this->x*v.y)-(v.x*this->y);
return *this;
}
bool vecteur::operator||(vecteur const& v){
vecteur temp;
temp.x=(this->y*v.z)-(this->z*v.y);
temp.y=(-this->x*v.z)+(v.x*this->z);
temp.z=(this->x*v.y)-(v.x*this->y);
//temp=this->x.operator^(v);
return (temp.x==v.x && temp.y==v.y && temp.z==v.z);
}
//Affichage
void vecteur::affiche(ostream& flux) const{
flux << x <<" , " <<y<<" , "<<z<<" , "<<endl;
}
//operateur d'injection de flux
ostream& operator<<(ostream& flux, vecteur const& vec){
vec.affiche(flux);
return flux;
} |
vecteur.h
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
#ifndef DEF_VECTEUR
#define DEF_VECTEUR
#include<iostream>
#include<string>
class vecteur{
private:
double x,y,z;
//int static nb_pts;
public:
//constructeurs
vecteur();
vecteur(double x,double y,double z);
vecteur(vecteur const& v);
//operateur arithmetique
vecteur& operator=(vecteur const& v);
double operator%(vecteur const& v);
vecteur& operator^(vecteur const& v);
vecteur operator+(vecteur const& v);
vecteur& operator+=(vecteur const& v);
bool operator||(vecteur const& v);
//operateur de comparaison
bool operator==(vecteur const& v);
//bool operator||(vecteur const& v);
//affichage
void affiche(std::ostream& flux) const;
~vecteur();
};
//Opérateur d'injection dans un flux
std::ostream& operator<<(std::ostream& flux, vecteur const& vec);
#endif |
:cry: