Forward Declaration error
Hi guys
I had a post going sometime before this error .. But could nt follow up on it.. Pardon me for that.. I am trying to do a bit of forward declarations in my program and wrote a couple of classes but then I run into this error which I do not seem to solve
src/BaseBodyDynamics.cpp: In member function 'void Xlib::BaseDynamics::State::RecalculateState()':
src/BaseBodyDynamics.cpp:15: error: invalid use of incomplete type 'struct Xlib::Quatf'
include/BaseBodyDynamics.h:21: error: forward declaration of 'struct Xlib::Quatf'
Pardon me if it is somethin trivial but then it would be nice for someone to chip in with a answer to why this is happening
..
I jus compiled all the code that I thought could be causing the error..
Code:
//test.h
class Quatf;
class Test{
//ctor
Test();
//dtor
~Test() ;
public:
struct state{
Quatf* myvariable;
void recalculate();
}
}
Code:
//test.cpp
#include "test.h"
void Test::state::recalculate(){
myvariable->
}
Code:
//Quaternion.h
template < class T>
class Quat
{
T x;
T y;
T z;
T w;
Quat(T _x, T _y,T _z, T _r) : x (_x),y(_y),z(_z),w(_w){}
Quat<T>& normalize();
T length();
};
inline Quat<T>& Quat<T>::normalize()
{
if ( T l = length() ) { r /= l; v /= l; }
return *this;
}
inline T Quat<T>::length()
{
return sqrt(x*x+y*y+z*z+w*w);
}
typedef Quat<float> Quatf;
I hope some one can help
Re: Forward Declaration error
In test.cpp you need to include the header that contains the definition of Quatf (i.e., Quaternion.h).
Re: Forward Declaration error
Thanks fellas,
Thanks for the reply but as i told it was jus a snippet .. and i forgot to write the inclusion i had made in the post... silly of me.. someone could have spot the error if i had writtern the question properly... In fact I had done the inclusion but ran into the error i had described jus so it myt be of use to someone else.
t was a very simple mistake that took two days off my time .. Quaternion depened on another vector class which was defined in a Vec3d.h file ...
While writing the cpp file for the BaseDynamics (the file throwing up errors) i have switched orders as in ..
I have writtern
#include "BaseDynamics.h"
#include "Quaternion.h"
#include "Vec3d.h"
But this is obviously wrong since basedynamics depened on quaternions and quaternions needed to know about vectors for determining its size .. The order of inclusion should have been exactly the reverse
#include "Vec3d.h" //this file doesnt depend on anything so comes first...
#include "Quaternion.h" //Depends on vec3d.h to be complete
#include "BaseDynamics.h" //depends on both the above files because it has both vectors and quaternions
Cheers,
Sundar
Re: Forward Declaration error
Sorry fellas,
Think i am jus running high on adrenalin today... I was wrong.. that was not the mistake.. well I thought it did fix it but then it throws up errors again..
src/BaseBodyDynamics.cpp:18: error: invalid use of incomplete type 'struct Xlib::Quat'
include/BaseBodyDynamics.h:21: error: forward declaration of 'struct Xlib::Quat'
scons: *** [obj/BaseBodyDynamics.o] Error 1
somebody help me!!!
;( sundar
Re: Forward Declaration error
Quote:
Originally Posted by sundar0206
But this is obviously wrong since basedynamics depened on quaternions and quaternions needed to know about vectors for determining its size .. The order of inclusion should have been exactly the reverse
To avoid this problem, #include "Vec3d.h" in #include "Quaternion.h" and #include "Quaternion.h" in #include "BaseDynamics.h". You should also be using compiler include guards.
Quote:
Originally Posted by sundar0206
well I thought it did fix it but then it throws up errors again.
What is the current code?
Re: Forward Declaration error
Hello there!
Quote:
I had a post going sometime before this error .. But could nt follow up on it.. Pardon me for that..
Aha! If you had, you'd have saved few tylenols!!
The reason you keep running into this problem is the confusion between
forward declaration and a typedef.
Always remember that a forward declration (a.k.a. incomplete type)
is not a "subsitution (or a shorthand)" for introducing a typename, i.e., when you do this
Code:
typedef Quat<float> Quatf;
and doyou're actually bringing about two different types into the picture,
former being a complete type and the latter being an incomplete type,
and that's what the compiler is telling you.
Following laserlight's suggestion would fix this error. (recommended)
or simply, get rid of class Quatf in the test.h file, and use a proper include #include "Quatf.h"
And you have several more errors such as the ones with the inlines, pointer, and the struct...
Please, don't forget to rate those who helped you~ :)
Bye~
Edit: added a few more noticable error
Re: Forward Declaration error
Quote:
Originally Posted by potatoCode
you're actually bringing about two different types into the picture,
former being a complete type and the latter being an incomplete type,
and that's what the compiler is telling you.
Following laserlight's suggestion would fix this error. (recommended)
or simply, get rid of class Quatf in the test.h file, and use a proper include #include "Quatf.h"
Good catch, but including the header is likely to be unnecessary. The correct forward declaration should be:
Code:
template<typename T>
class Quat;
typedef Quat<float> Quatf;