1.11M Members

Correctly including source files?

 
1
 

I'm having a lot of trouble getting my code to compile.

It basically has this structure:

Headers: a.h, b.h, c.h
CXX: a.cxx, b.cxx, c.xx

And my main.cpp of course.

The problem is that my classes link each other, ie. a uses something from class b and b uses something from class a.

I know that cxx files should #include their corresponding .h file but I simply cannot get it to work.

The code works if i put it in one huge file but simply won't compile if I use different files like I said above.

Thank you very much.

 
0
 

Won't compile or won't link?

 
0
 

I can think of 2 possible issues here:

  1. Unguarded headers causing multi-include errors
  2. Circular definition errors that can't be resolved because of how your code is currently written.

You'll have to describe the problem better and post the relevant code.

 
0
 

Won't compile:

invalid use of incomplete type struct a

My headers do contain the ifndef to prevent multi include problems.

 
0
 

a struct prototype/forward declaration may solve your woes, or a different design that avoids circular dependencies.

struct a;

 
0
 

They are all classes, and I have tried adding that.

As far as I know, the problem is that it complains because it knows it is a class but does not find the member functions.

So, how should I #include files?

 
0
 

Just remember to include the file which you are trying to use is declared in it.
For example, if you use Class A in x.cpp, include the header file where the Class A is declared. This principle also fit for function declarations.
However, for global variables, if you need to use them in another cpp file, you need to have them externed.

 
0
 

Or you can let us view the entire project.

 
0
 

All these random generic hints at your problem aren't helping us help you with it. Please be specific and provide details. The appropriate details include:

  1. The actual error codes being returned by the compiler/linker
  2. The actual code relevant to the issue
  3. A description of the intent of your code
You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: