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.

Recommended Answers

All 8 Replies

Won't compile or won't link?

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.

Won't compile:

invalid use of incomplete type struct a

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

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

struct a;

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?

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.

Or you can let us view the entire project.

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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.