lundi 26 avril 2021

error: call to 'CrossProduct' is ambiguous

I have created a template class named Vector in a header file, say foo.h. Here is how I defined the template class

template <class T, int dimensions> class Vector {
  private:
    T component_[dimensions];
  public: 
    Vector(const T& a = 0) {
      for (int i = 0; i < dimensions; i++)
        component_[i] = a;
    } // default/scalar constructor
    Vector(const Vector& v) {
      for (int i = 0; i < dimensions; i++)
        component_[i] = v.component_[i];
    } // copy constructor
    ~Vector() {;} // destructor

    void set(int i, const T& a) {
      component_[i] = a;
    } // change ith component of the vector
}; 

... and few other member functions. Then I created an alias for 3D vector as follows

typedef Vector<double, 3> Point3D;

Following this, I declared few function prototypes for 3D vectors

Point3D CrossProduct(const Point3D&, const Point3D&);
double ScalarTripleProduct(const Point3D&, const Point3D&, const Point3D&);
Point3D VectorTripleProduct(const Point3D&, const Point3D&, const Point3D&);

This is the end of foo.h. I then defined these three function in foo.cpp. Here is the code below.

#include "foo.h"

Point3D CrossProduct(const Point3D& a, const Point3D& b) {
  Point3D c;
  c.set(0, a[1] * b[2] - a[2] * b[1]);
  c.set(1, a[2] * b[0] - a[0] * b[2]);
  c.set(2, a[0] * b[1] - a[1] * b[0]);
  return c;
} // vector cross product: a X b, defined only for 3D vectors                                                                                                                                                                                                               
double ScalarTripleProduct(const Point3D& a, const Point3D& b,
    const Point3D& c) {
  return a * CrossProduct(b, c);
} // scalar triple product: a . (b X c), defined only for 3D vectors

Point3D VectorTripleProduct(const Point3D& a,
    const Point3D& b, const Point3D& c) {
  return CrossProduct(a, CrossProduct(b, c));;
} // vector triple product: a X (b X c), defined only fr 3D vectors

Now when I try to compile foo.cpp, I get the following errors

foo.cpp: error: call to 'CrossProduct' is ambiguous
        return a * CrossProduct(b, c);
                   ^~~~~~~~~~~~
./foo.h: note: candidate function
        Point3D CrossProduct(const Point3D&, const Point3D&);
                ^
foo.cpp: note: candidate function
Point3D CrossProduct(const Point3D& a, const Point3D& b) {
        ^
foo.cpp: error: call to 'CrossProduct' is ambiguous
        return CrossProduct(a, CrossProduct(b, c));;
                               ^~~~~~~~~~~~
./foo.h: note: candidate function
        Point3D CrossProduct(const Point3D&, const Point3D&);
                ^
foo.cpp: note: candidate function
Point3D CrossProduct(const Point3D& a, const Point3D& b) {
        ^

I am not sure about what went wrong. Could you please help me with this? Thank you.

Aucun commentaire:

Enregistrer un commentaire