We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,371 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Correctly including source files?

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.

4
Contributors
8
Replies
6 Hours
Discussion Span
2 Years Ago
Last Updated
9
Views
juanp
Newbie Poster
3 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Won't compile or won't link?

pseudorandom21
Practically a Posting Shark
890 posts since Jan 2011
Reputation Points: 216
Solved Threads: 110
Skill Endorsements: 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.

Fbody
Posting Maven
2,929 posts since Oct 2009
Reputation Points: 833
Solved Threads: 394
Skill Endorsements: 5

Won't compile:

invalid use of incomplete type struct a

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

juanp
Newbie Poster
3 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

struct a;

pseudorandom21
Practically a Posting Shark
890 posts since Jan 2011
Reputation Points: 216
Solved Threads: 110
Skill Endorsements: 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?

juanp
Newbie Poster
3 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.

deltamaster
Newbie Poster
11 posts since Jun 2009
Reputation Points: 10
Solved Threads: 2
Skill Endorsements: 0

Or you can let us view the entire project.

deltamaster
Newbie Poster
11 posts since Jun 2009
Reputation Points: 10
Solved Threads: 2
Skill Endorsements: 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
Fbody
Posting Maven
2,929 posts since Oct 2009
Reputation Points: 833
Solved Threads: 394
Skill Endorsements: 5

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0899 seconds using 2.81MB