Hello everyone,

I don't have much experience with C++ and it has been a while since I have last used it. Right now I am writing my own class for quaternions. My problem which I cannot figure out is what is wrong with the operator overload shown below... No matter what I try and change I more or less recieve multiples of the same error:

invalid operands of types `double*' and `double*' to binary `operator*'

I've looked around and don't really understand what my problem is... Any help is much appreciated.

Thanks in advance.

class CQuaternion{
      public:
             // Constructors
             CQuaternion(double,double,double,double);
             CQuaternion();
             
             // Desctructors
             ~CQuaternion();  
             
             CQuaternion* operator*(CQuaternion&);  
             void conjugate ();
             void print () ;
                      
      private:    
             double *w, *x, *y, *z;                   
};

CQuaternion::CQuaternion (double a, double b, double c, double d) {
  *w = a;
  *x = b;
  *y = c;
  *z = d;
}

CQuaternion::CQuaternion () {
  w = new double;
  x = new double;
  y = new double;
  z = new double;
  
  *w = 0;
  *x = 0;
  *y = 0;
  *z = 0;
}

CQuaternion::~CQuaternion () {
  delete w;
  delete x;
  delete y;
  delete z;
}

void CQuaternion::conjugate() {
  *x = -(*x);
  *y = -(*y);
  *z = -(*z);
}
CQuaternion* CQuaternion::operator* (CQuaternion& param) {
  CQuaternion* temp;
  
  temp->w = (this->w)*param.w - (this->x)*param.x - (this->y)*param.y - (this->z)*param.z;
  temp->x = (this->w)*param.x + (this->x)*param.w + (this->y)*param.z - (this->z)*param.y;
  temp->y = (this->w)*param.y - (this->x)*param.z + (this->y)*param.w + (this->z)*param.x;
  temp->z = (this->w)*param.z + (this->x)*param.y - (this->y)*param.x + (this->z)*param.w;
  
  return (*temp);
}

Recommended Answers

All 2 Replies

Ughhh, why are you using pointers? None of that is necessary. None of it!


Look at your CQuaternion* operator*(CQuaternion& param), it returns a pointer
to CQuaternion, but look at what you are returning in that function, return *temp .

Whats even worse is that this code :

CQuaternion* temp;
 
  temp->w = (this->w)*param.w - (this->x)*param.x - (this->y)*param.y - (this->z)*param.z;
  temp->x = (this->w)*param.x + (this->x)*param.w + (this->y)*param.z - (this->z)*param.y;
  temp->y = (this->w)*param.y - (this->x)*param.z + (this->y)*param.w + (this->z)*param.x;
  temp->z = (this->w)*param.z + (this->x)*param.y - (this->y)*param.x + (this->z)*param.w;

is very wrong because you never allocate space for temp. Because its a pointer,
you need to allocate space for it via new operator. But you use it by setting its
x,y,z values. Thats an undefined behavior.

And the original error you are getting is because you are trying to multiply a pointer against a pointer. Specifically in this
code (this->w)*param.w . That basically says this :

int *x = new int(0);
int *y = new int (1);
int value = x * y; //not valid since x and y are pointers
//what you are looking for
int value2 = *x * *y; //you need to deference the pointer

I suggest you to remove all that pointer. In fact your CQuaternion should look something like this :

class CQuaternion{
             // Constructors
             CQuaternion(double w = 0,double x = 0,double y = 0,double z = 0);
             CQuaternion operator*(const CQuaternion&) const;
             CQuaternion conjugate()const;
             void print () ;                      
      private:    
             double w, x, y, z;                   
};

CQuaternion::CQuaternion (double a, double b, double c, double d) 
: w(a), x(b) , y(c), z(d)
{}
void CQuaternion::conjugate()const{
  return CQuaternion(-w,-x,-y,-z);
}
CQuaternion CQuaternion::operator* (const CQuaternion& param)const{  
  return CQuaternion(w*param.w - x*param.x - y*param.y - z*param.z,
					 w*param.x + x*param.w + y*param.z - z*param.y,
					 w*param.y - x*param.z + y*param.w + z*param.x,
					 w*param.z + x*param.y - y*param.x + z*param.w);
    
}

From the looks of it, I would suggest that conjugate() and operator* should be
a global function. They are not necessarily needed in the CQuaternion class.

Couple of small points, since FirstPerson is correct. You should not be using pointers in your quaternion class, but I believe that FP has made a tiny tiny mistype:

Conjugate of a quaternion is normally

#
void CQuaternion::conjugate()const
{
return CQuaternion(w,-x,-y,-z);    // w is not multiplied by -1 here.
}

Depending on your code, I prefer to have operator*= called from operator* and make
operator*= a member of the class, and think about putting operator* either in / out of the class as is suitable.

However, It think conjugate would normally be a member of the class, because you normally would write this,

Quaternion Q(3,-4,5,-6);
double Weight=(Q * Q.conj()).real();
// or more typically:
result = Q.conj() * func() * Q;

and that leaves one less place to go and update, if you say refactor the class to use a double + a 3d vector.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.