Error: field is incomplete type

quaternion.h: 15: error: field 'v is of an incomplete type

Hello! I am stuck on an error that I cannot 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 =)

+6
c ++ incomplete-type
source share
3 answers

Well, you have the circular inclusion of two header files: vec3.h and quaternion.h . Turn on the guards, make sure that each heading is turned on only once. One of them will be included first, the second - the second. In your case, quaternion.h included first, which means that Vec3 becoming incomplete. This is what the compiler tells you.

Because you are trying to use the Vec3 object as a direct member of the Quaternion object, you absolutely need Vec3 be a full type. The quaternion.h header should include the vec3.h header.

 class Vec3; 
Announcement

does nothing in quaternion.h , so you can just delete it.

Given the above, it follows that vec3.h cannot include quaternion.h , or you will get a circular inclusion that never achieves anything. Remove inclusion quaternion.h from vec3.h Save

 class Quaternion; 

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

+10
source share

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

Then quaternion.h may include vec3.h and everything will compile. Also, as JaredPar suggested, remove the front- Vec3 in quaternion.h .

+3
source share

the problem is that your files include "eachother" where there is no circular dependency. vec3 should not include quaternion, then everything will be fine.

now the error provided by the compiler is that you previously declared vec3, but the full definition is not readable if vec3 is enabled:

vecr3.h → enable quaternion
quaternion.h -> enable vec3, but enable guards, so nothing happens
quaternion.h → predeclare Vec3,
quaternion.h → try using vec3 // fail
vec3.h → actually declare vec3

the only correct order is the first vec3, and only THEN includes the quaternion.

0
source share

All Articles