LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-09-2006, 06:37 PM   #1
allomeen
Member
 
Registered: Dec 2005
Posts: 83

Rep: Reputation: 15
Circular Header files problem


Hello,

I have a problem and I don't know if there is any solution to it. I have three classes: Obj1, Obj2 and Obj3. Obj1 includes Obj2 and Obj2 includes Obj3, and Obj3 includes Obj1. for example:

//Obj1.h
#include "Obj2.h"

//Obj2.h
#include "Obj3.h"

//Obj3.h
#include "Obj1.h"

Whenever I compile, I get a lot of errors saying that Obj1 is not declared wherever I use Obj1. There are a circular dependency and I'm wondering is there any way i can solve this problem, without changing the structure of the program??

Thanks,
Alaa
 
Old 02-09-2006, 07:41 PM   #2
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
use forward prototypes or include them in the same header/namespace (if this fits the design)
 
Old 02-09-2006, 11:59 PM   #3
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Rep: Reputation: 32
Two ways:
(1):
Wrap each header file in an #ifndef, like so:
myheader.h:
Code:
#ifndef _MYHEADER_H_
#define _MYHEADER_H_

//Contents here

#endif
//EOF
This should solve most problems, and you should always do this, no matter what the situation.

The second way, which is sometimes needed, is to prototype the class, function, or datatype being used. Like so:
Code:
int MyFunc(int); // Prototype
class B; //Prototype

class A
{
    public:
        bool MyBool;
        B MyB;
};

class B //Declaration
{
    public:
        bool YetAnotherBool;
};

int main(int argc, char **argv) //Declaration
{
}

int MyFunc(int iI) //Declaration
{
    //Do something
}
Hope that helps. By the way, if you have a multi-file project, you might want to read up on the "extern" keyword.
 
Old 02-10-2006, 11:34 AM   #4
allomeen
Member
 
Registered: Dec 2005
Posts: 83

Original Poster
Rep: Reputation: 15
Thank you guys, I do have this in the header fils. The way i solved the problem is by moving some of the include header to the ".cpp" file so I don't get the circular definitions and it works beautifully.. Thanks anyway.
 
Old 02-10-2006, 01:44 PM   #5
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
You should still do the "#if !defined(_MY_HEADER_H)" ...

regardless of whether or not you got past the immediate problem, The_nerd's advice is good advice - you should definitely do this with all of your headers.

IMHO ...
 
Old 02-11-2006, 06:59 PM   #6
allomeen
Member
 
Registered: Dec 2005
Posts: 83

Original Poster
Rep: Reputation: 15
I already have those in the header file. That was not the problem. The problem was where I define it. I had to define it in the .cpp file instead.

Thanks,
Alaa
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Including header files and source files for classes Feenix Programming 8 09-28-2005 10:53 AM
Weird circular apt install problem rosslaird Debian 2 01-14-2005 12:12 PM
vmware problem with kernel C header files on SuSE 9.1 spariggio82 Linux - General 12 09-02-2004 03:19 PM
c header files in linux in place of header files in windows? harun_acs Programming 1 03-17-2004 02:24 AM
Problem with C++ include/header files! Pisces107 Programming 12 12-23-2003 11:06 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration