[C++] operateur inconnue

Bj à tous
Voila, j'ai rencontrer sur pas mal de sources l'opérateur "^"(dont un bouquin de Stroustrup) et je ne sais pas à quoi il sert...
si je l'utilise le compilo me renvoi une erreur :
nvalid operands of types `double' and `double' to binary `operator^'
et impossible de le surcharger, le compilo renvoi aussi une erreur...
est-ce que quelqu'un a des informations la dessus? car il pourrai être bien utile ce symbole pour faire du produit scalaire ou vectoriel ou de convolution.

Salutation !
Char Snipeur

5 réponses

  1. ^ correspond au OU exclusif de l'Algèbre de Bool
    0
    1. Hello !

      Exemple de définition d'opérateur ^ :
      struct vecteur
      {
      	int x, y, z;
      };
      
      inline
      vecteur operator^(const vecteur& A, const vecteur& B)
      {
      	vecteur R = { A.y * B.z - A.z * B.y,
      	              A.x * B.z - A.z * B.x,
      	              A.x * B.y - A.y * B.x};
      	return R;
      }
      
      int main()
      {
      	vecteur A = { 1, 2, 3 };
      	vecteur B = { 2, 3, 0 };
      	vecteur P = A ^ B;
      	cout << P.x << " " << P.y << " " << P.z << endl;
      }
      


      À noter qu'il n'est possible de redéfinir les opérateurs du langage. Par exemple, il est interdit de définir l'opérateur suivant :
      int operator^(int X, int Y); // NOK, déjà fourni par le langage
      

      Take care !
      0
      1. Contributeur
        Merci bien à vous deux. Une dernière petite precision.
        Si on fait un typedef int entier
        peut on redefinir
        entier operator^(entier x, entier y);

        ?

        Salutation !
        Char Snipeur
        0
        1. Hello !

          Non, cela ne change rien. Typedef ne crée qu'un alias.
          Pour s'en sortir :
          #include <iostream>
          
          using namespace std;
          
          struct entier
          {
          	int X;
          
          	entier(int I) : X(I) {}
          	operator int() const { return X; }
          };
          
          inline
          entier operator^(const entier& A, const entier& B)
          {
          	return A + B;
          }
          
          inline
          entier operator^(const entier& A, const int& B)
          {
          	return A + B;
          }
          
          inline
          entier operator^(const int& A, const entier& B)
          {
          	return A + B;
          }
          
          inline
          ostream& operator<<(ostream& Flux, const entier& V)
          {
          	return Flux << V.X;
          }
          
          
          int main()
          {
          	entier A = 1;
          	entier B = 2;
          
          	cout << (A ^ 3) << endl;	// 4
          	cout << (7 ^ B) << endl;	// 9
          	cout << (A ^ B) << endl;	// 3
          }
          

          Take care !
          0
          1. L'opérateur :

            ostream& operator<<(ostream& Flux, const entier& V)


            ne sert à rien. Peut être supprimé.
            0