1,105,644 Community Members

Correctly including source files?

Member Avatar
juanp
Newbie Poster
3 posts since Feb 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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.

Member Avatar
pseudorandom21
Practically a Posting Shark
888 posts since Jan 2011
Reputation Points: 166 [?]
Q&As Helped to Solve: 115 [?]
Skill Endorsements: 0 [?]
 
0
 

Won't compile or won't link?

Member Avatar
Fbody
Posting Maven
2,845 posts since Oct 2009
Reputation Points: 682 [?]
Q&As Helped to Solve: 393 [?]
Skill Endorsements: 7 [?]
Featured
 
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.

Member Avatar
juanp
Newbie Poster
3 posts since Feb 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Won't compile:

invalid use of incomplete type struct a

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

Member Avatar
pseudorandom21
Practically a Posting Shark
888 posts since Jan 2011
Reputation Points: 166 [?]
Q&As Helped to Solve: 115 [?]
Skill Endorsements: 0 [?]
 
0
 

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

struct a;

Member Avatar
juanp
Newbie Poster
3 posts since Feb 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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?

Member Avatar
deltamaster
Newbie Poster
11 posts since Jun 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
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.

Member Avatar
deltamaster
Newbie Poster
11 posts since Jun 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

Or you can let us view the entire project.

Member Avatar
Fbody
Posting Maven
2,845 posts since Oct 2009
Reputation Points: 682 [?]
Q&As Helped to Solve: 393 [?]
Skill Endorsements: 7 [?]
Featured
 
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 three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: