Forward Declaration error

Help need with c++ forward declarations
I am new to using forward declarations. I thought I had understood quite a bit of it until I got this error. Seems there is something really really basic that I miss. can some one of you help me with this error???


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'


basically i tried forward declaring the class Quat . Then had a variable declared in the header file which was a pointer. When i try using the variable in the cpp file after including Quaternion.h which is the parent file of Quat I get this error.

A small code snippet
1
2
3
4
5
6
7
//Header file
class Quatf;
Quatf* m_qSObjectSpin;

//cpp file in the RecalculateState function
m_qSObjectSpin->normalize();

Actually the quaternion is a part of the struct i have defined in my class..I am not sur eif that is causing the error or if there is any better way to do this.

Cheers
Last edited on
you are forward declaring Quat but the function returns Quatf. Is that a typo?
ah yes.. it is meant to be Quat f.. will correct it...
Quat is a templated class i have defined .. so Quatf i have typedef with a float as the template
 
typedef Quat<float>	Quatf;


and Quat<T> is the original class

The code compiles fine if i don add the normalize method... Any Quat variable i define when instanced gives me the same error
Last edited on
A forward declaration allows you to declare a variable of pointer type, but before you actually use it the compiler must see the complete definition. The error message indicates that this is not happening.
how do i really get to solve this issue??
This sounds like it might be a template implementation issue.

The function bodies for a template class cannot exist in a seperate .cpp file*, then need to all exist in the header file as if they were inlined*.

This has to do with templates not being actual classes, and not being output to the linker until their instantiated, which they can't be unless the source file instantiating them can see the implimentation, which it can't if it's in a seperate file.

* footnote: there are ways around this, so this isn't an absolute rule, but you get the idea.

If that's not your problem I'd need to see more code. If you could paste a minimal example on the boards here (preferably something small, that compiles, and reproduces the problem) I could be of more help.
Sorry for the real late reply fellas,

As for the Disch's suggestion. I tried to test by allocating the variable not as a pointer but just as a instance. But I seem to get a error that the instance is not defined when I try to do somthing like m_qSObject.normalize();


1
2
3
4
5
6
7
8
9
10
11
12
13
14
//test.h
class Quatf;

class Test{
 //ctor
 Test();
//dtor
~Test() ;
public:
struct state{
  Quatf* myvariable;
  void recalculate();
}
}

1
2
3
4
5
6
//test.cpp
#include "test.h"

void Test::state::recalculate(){
   myvariable-> 
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//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 think these are the code bits causing a error for me .. I hope someone can help
Last edited on
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
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
Topic archived. No new replies allowed.