Jump to content
  • Advertisement
Sign in to follow this  
Soth

Linking many files with a single header

This topic is 4564 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Consider I have the following code that links succesfully:
//file vector.h

#pragma once

class Vector
{
.
.
};

//file matrix.h
#pragma once
#include "vector.h"

class Matrix
{
.
.
Vector Mult( Vector &a);
};

//file quat.h
#pragma once
#include "Matrix.h"

class Quat
{
.
.
Vector Mult( Vector &a);
};

Now if i write
// file lib.h
#pragma once

#include "Vector.h"
#include "Matrix.h"
#include "Quat.h"

//file vector.h

#pragma once

class Vector
{
.
.
};

//file matrix.h
#pragma once
#include "lib.h"

class Vector;

class Matrix
{
.
.
Vector Mult( Vector &a);
};

//file quat.h
#pragma once
#include "Lib.h"
class Vector;
class Matrix;

class Quat
{
.
.
Vector Mult( Vector &a);
};

I get tons of linker errors saying "use of undefined class".How to make it link? [Edited by - Soth on April 18, 2006 5:33:53 PM]

Share this post


Link to post
Share on other sites
Advertisement
It's hard to read that. Try using [ code] or [ source] tags.

As for your problem, a few things:

- Try rebuilding the application (i.e. in .NET, clean and build).
- When I need forward declarations, I put the forward declarations of the classes in the .h file (like you are doing), and I include the necessary .h files in the .cpp file. This can possibly save you some compile time, and it will force you to keep your implementation in the source files.
- I have had issues in the past with #pragma once. You could give the more standard, #ifndef/#define/#endif blocks a try.

I don't see a glaring error, but maybe someone else does. Give these suggestions a try and see if that fixes your problem.

Share this post


Link to post
Share on other sites
You basically have a mess. Matrix.h includes lib.h, which includes Quat.h which includes lib.h, which is already included, but includes Matrix.h which is also already included. At some point Vector.h is included, but is it before or after it is needed?

The solution? Stick with your first version and try replacing the includes in the header files with forward declarations. Try to follow these rules:
  • Don't use a header file that includes all the other header files (except when using pre-compiled headers). It will lead to problems (such as the ones you are having).
  • Header files should be self-sufficient. That is, they include whatever files they need in order to compile.
  • Every file (both header and source files) should include exactly the files that they need -- no more, no less. Strict adherence to this is time-consuming, so laziness is acceptable (up to a point).
  • In header files, use forward declarations whenever possible.
  • The first file included in a source file should be its header file (except when using pre-compiled header files). This helps ensure that header files are self-sufficient.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!