views:

38

answers:

4

Hi! I have gotten rid of a circular dependence but am still having issues with another problem.

I am still learning and hope that someone can explain to me more about what is wrong with my implementation. Sorry for the trouble, but I really appreciate everyone who is helping me.

So, the issue I am having now is that in my Vec3 class, it is trying to use Quaternions, but it does not have the "complete type" of the quaternion.

The following is what I have:

vec3.h

#ifndef VEC3_H

#define VEC3_H



#include "point.h"

#include <math.h>

class Quaternion;


class Vec3

{

    friend ofstream& operator <<(ofstream& output, const Vec3& p);

    friend ifstream& operator >>(ifstream& input, Vec3& p);



    public: 

    Vec3();

    Vec3(double _x, double _y);

    Vec3(double _x, double _y, double _z);



    double x,y,z;



    //Operators

    Vec3 operator -(Vec3 a) const;

    Vec3 operator /(double s) const;

    Vec3 operator *(double s) const;

    Vec3 operator *(Quaternion q) const;



    // Used to do vector Vec3 addition

    Vec3 operator +(Vec3 a) const;

    Point operator +(Point a) const;



    Vec3& operator =(Point a);



    Vec3 crossProduct(Vec3 v1); // Itself cross v1

    double dotProduct(Vec3 v);

    double length();

    void normalize();

};



#endif

Quaternion.h

#ifndef QUATERNION_H    
#define QUATERNION_H

#include "vec3.h"


class Quaternion

{



public:

    Quaternion(Vec3 v);

    Quaternion(double w, Vec3 v);



    Vec3 v;

    double scalar;



    Quaternion operator *(Quaternion s);

    Quaternion conjugate();

};



#endif

vec3.cpp The error is on each line of this function.

...    

Vec3 Vec3::operator *(Quaternion q) const

{

    // Change this vector into a quaternion

    Quaternion currentQuat(0, Vec3(x,y,z));

    Quaternion newQuat = currentQuat*q;

    Quaternion result = q.conjugate()*newQuat;



    return result.vec();

}

...
+1  A: 

In the cpp file you should include the header for Quaternion.

The forward declarations in the headers just help you to get all declarations right, so that declarations don't require each other to be fully available.

To use a type, you'll need to actually include the header.

UncleBens
+2  A: 

You need to add

 #include "Quaternion.h"

to the top of vec3.cpp.

eduffy
Thanks, wow I forgot I removed quaternion.h in the .h file. Thanks alot =)
Aero
+1  A: 

If you only have a forward declaration in the header file, you can only use the type in argument lists as a pointer or reference, so change your

Vec3 operator *(Quaternion q) const;

to

Vec3 operator *(const Quaternion &q) const;

John Gordon
+1  A: 

A forward declaration to a type is enough to use a pointer to that type.

class Quaternion;

void func(Quaternion* q);

However, as soon as you use the type directly, the forward declaration is no longer enough.

void func(Quaternion q);

For the compiler to use the type rather than a pointer to the type, it needs the full type declaration rather than just a forward declaration. In this case, you have a forward declaration for Quaternion but not the full declaration, so the compiler gives the error that the type Quaternion is incomplete. The solution is to give vec3.h the full type declaration for Quaternion by including Quaternion.h.

By the way, it's a bit odd that one of your header files is capitalized and the other isn't. You might want to choose a more consistent naming scheme.

Jonathan M Davis