static library woes

S

sklett

I am trying to make a library that will read and write one of our text file
formats. It is a hierarchical structure and I have modeled the classes
after it. For example:
class Character
class Skeleton
class Keyframe

Where a Character has a vector of Skeleton objects and a Skeleton has a
vector of Keyframe objects. All of these classes are in separate h/cpp
files. I use typedef to define a type that represents a std::vector of the
various objects. I create these typedefs after the class declaration in the
h file.
I have run into a real nightmare with all these interrelated classes. I
have a mess of #include statements just trying to get everything to compile.
Some h files need a class prototype just to compile, even though I have
added the relevant class' h file. For example

//Character.h
#include "skeleton.h"
class Skeleton; // without this line, it won't compile because of
the use of Skeleton in the ctor of Character

class Character
{
Character(const Skeleton& skel);
}


Sometimes this doesn't work. It's so frustrating when I include the file of
the type being used and the compiler still doesn't know what it is. I know
that I'm supposed to limit #include statements in h files, but many of my
functions have parameters that use my various classes, so I need to. In
some rare cases, including a used class' h file in the cpp file of the class
that's using it will work, but it's very hit and miss. It just feels
totally random.

So, there is my sob story. Now I want to know what I'm doing wrong.
Assuming you were writing a static library with many interrelated files,
what are some general rules of tricks for header inclusion? I've tried to
think of it sequentially as the compiler might approach it, but this hasn't
helped.

I am using precompiled headers. I have included all my stl stuff, disabled
warnings, created macros, etc... but I haven't added any of my file headers,
this seems wrong.

Suggestions? Tips? Anything? I'm really stuck...

Thanks for reading,
Steve
 
S

sklett

Just realized I posted this in .net NG, sorry, I will go post in the
"dangerous" C++ NG :)
 
H

Holger Grund

sklett said:
Where a Character has a vector of Skeleton objects and a Skeleton has a
vector of Keyframe objects. All of these classes are in separate h/cpp
files. I use typedef to define a type that represents a std::vector of
the various objects. I create these typedefs after the class declaration
in the h file.
That's the correct way. A class type X must be complete before
std::vector said:
I have run into a real nightmare with all these interrelated classes. I
have a mess of #include statements just trying to get everything to
compile. Some h files need a class prototype just to compile, even though
I have added the relevant class' h file. For example

//Character.h
#include "skeleton.h"
class Skeleton; // without this line, it won't compile because of
the use of Skeleton in the ctor of Character

You probably don't have the appropriate include guards. Typically,
you'd say
// start-of-file
#ifndef CHARACTER_H_INCLUDED
#define CHARACTER_H_INCLUDED
// definitions
#endif // end-of-file

You can then simply put the appropriate includes without
running into cyclic dependencies.

The most straightforward way is probably to have a separate
header with forward declarations and possibly (enums &
typedefs).

Make sure it doesn't include anything else and you include
it as the first file from every other header.

Then only add #include in headers if complete objects are
required.

In your case
Character.h includes forward.h, Skeleton.h
Skeleton.h includes forward.h, Keyframe.h
Keyframe.h includes forward.h

-hg
 
S

sklett

Thank you Holger,

I missed this message before. I like your idea about using a forward file.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top