views:

30

answers:

3

quaternion.h:15: error: field ‘v’ has incomplete type

Hi! I am stuck on an error that I cannot seem to solve.

Below is my code:

#ifndef QUATERNION_H
#define QUATERNION_H

#include "vec3.h"

class Vec3;

class Quaternion
{

public:

 Quaternion(Vec3 v);

 Quaternion(double w, Vec3 v);

 Vec3 v; <--------------------------This is where the error is :(

 double scalar;



 Quaternion operator *(Quaternion s);

 Quaternion conjugate();

};



#endif

My Vec.h looks like this:

#ifndef VEC3_H

#define VEC3_H



#include "point.h"

#include "quaternion.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

Thanks for the help again =)

A: 

the problem is that your files are include'ing eachother where there's no circular dependency. vec3 should not include quaternion then everything 'll be alright.

now the error the compiler gave is because you predeclared vec3 but the full definition is not read if vec3 is included:

vecr3.h --> include quaternion
quaternion.h --> include vec3 , but include-guards so nothing happens
quaternion.h --> predeclare Vec3,
quaternion.h --> try to use vec3 //fail
vec3.h --> actually declare vec3

the only correct order is first vec3 include and only THEN the quaternion include.

flownt
A: 

The problem is mutual inclusion of the .h. The compiler knows the types, but at some point they are incomplete. My advice would be to just forward declare Quarternion in the file vec3.h, but NOT include quaternion.h.

Then, quaternion.h can include vec3.h and everything will compile. Also, as JaredPar suggested, remove the forward declaration of Vec3 in quaternion.h.

Diego Sevilla
A: 

Well, you have circular inclusion of two header files: vec3.h and quaternion.h. Include guards will make sure that each header is included only once. One of them will be included first, the other - second. In your case quaternion.h is included first, meaning that Vec3 becomes an incomplete type in it. This is what the compiler is telling you.

Since you are trying to use Vec3 object as an immediate member of Quaternion object, you absolutely need Vec3 to be a complete type. The quaternion.h header must include vec3.h header. The

class Vec3;

declaration achieves absolutely nothing in quaternion.h, so you can just remove it.

Given the above, it follows that vec3.h cannot include quaternion.h, or you'll end up with circular inclusion, which never achieves anything. Remove the inclusion of quaternion.h from vec3.h. Keep the

class Quaternion;

declaration in vec3.h and see if it works that way.

AndreyT