# Problem in a dll

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

## Recommended Posts

So I have this class that was defined in a header(along with another class),but i decided i should create another header just for that class.

So now the class has it's own header and it's own cpp file.

I included it in all the files that need it.

The problem is,in the file where it was defined the first time(that header file),I have a global variable of it's type.And right now i get an error saying that the class it's not defined.

The header of the class is included,i also tried prototipying the class but still nothing.

BTW: I use header guards for all the classes.

What could be wrong?

##### Share on other sites

I'd guess you've got a circular inclusion problem, though obviously it's hard to say anything close to definitive if you don't post any code.

##### Share on other sites
The guard *causes* the problem.  Let's say files A and B include each other.  The preprocessor starts working on file A.  File A's header guard #define is defined, then it includes file B.  File B's header guard is defined, then includes file A.  File A's #ifdef fails, which skips the rest of file A.  The rest of file B continues... and can't find a definition from file A because the second pass of file A got skipped.

In other words, the preprocessor sees this:

#ifndef FILE_A
#define FILE_A
#include "fileb.h"
// (including file B)
#ifndef FILE_B
#define FILE_B
#include "filea.h"
// (including file A)
#ifndef FILE_A
// (ifndef fails)
#endif
// (declarations from File B, which cannot find the declarations from file A)
#endif
// (declarations from File A)
#endif

The usual way to solve this problem is to forward declare things instead of #including when possible.

For global variables, you should put 'extern' on the header declaration(s) and define the variable once in one CPP file. Edited by Nypyren

##### Share on other sites

That's probably the problem then....how should i go about solving this? Just deleting the guards will probably cause other problems(if not now,in future for sure)

This is kinda stupid,cause the way i see it,it fails,even though A is already there! Why is that?

Edited by noatom

##### Share on other sites

How you fix it depends on what actual code you have. It could involve removing unnecessary include directives from headers, forward declaring classes, breaking headers into smaller headers and so on.

##### Share on other sites

You'll need to post your code for us to give you guided assistance.  If you can't (for example, confidential code), then my only suggestion is to read up on forward declaration and the difference between declarations and definitions *very* thoroughly.

Edited by Nypyren

##### Share on other sites

problem solved...i had about 4 variables defined in a header...i used extern on them and defined them in the cpp file.

• 21
• 11
• 9
• 17
• 13