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

## 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 on other sites
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 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 on other sites
Thanks 2 all , the problem is solved.

1. 1
2. 2
3. 3
Rutin
22
4. 4
5. 5

• 12
• 19
• 14
• 9
• 9
• ### Forum Statistics

• Total Topics
632929
• Total Posts
3009286
• ### Who's Online (See full list)

There are no registered users currently online

×