Solved

inlude file problem

Posted on 2001-06-15
21
440 Views
Last Modified: 2010-05-18
Hi,

I have two classes say class1 and class2 in two different files i.e I have for files

Class1.cpp
Class1.h
Class2.cpp
Class2.h

Now class1 has to use class2 and vice versa, I cann't include files like, class1.h includes class2.h and class2.h include class1.h, my friend told me to include class1.h in class2.h and just declare class1 before the class2 something like

//class2.h
#include "class1.h"
class class2{
????
};

//class1.h

class class2;
class class1{
???..
???..
};

but it didn't work for me

one more thing how can I write an if statement to check that a specified is already included or not, I know it is some thing like #IF_DEF stuff needs exact syntax.
0
Comment
Question by:newborn
  • 13
  • 5
  • 2
  • +1
21 Comments
 
LVL 30

Expert Comment

by:Axter
Comment Utility
The #if your talking about are called header gaurds.
They would look like this in your header file:

#ifndef MY_FOO_H_FILE
#define MY_FOO_H_FILE

class foo
{
//class code ....
};

#endif //MY_FOO_H_FILE
0
 
LVL 7

Expert Comment

by:KangaRoo
Comment Utility
If Class1 needs to 'know' about Class2 and Class2 needs to 'know' about Class1 then we use forward declarations. A forward declaration basically tells the compiler that a typename (or class name) exists. Nothing more.
Which is what your friend told you to do.

Forward declarations only tell the compiler that a type (class) exists, it still doesn't know what members it has. This limits the of a forward declared type to pointers

//class2.hpp
#ifndef CLASS2_HPP
#define CLASS2_HPP

class Class1; // forward declaration
class Class2
{
  Class1* ptr;
  Class1  instance; // can't do this

  public:
     foo() {
        ptr->member();  // can't do this either
     }

     bar(Class1* p);  // declaring is fine
}
#endif


// class1.hpp
#ifndef CLASS1_HPP
#define CLASS1_HPP

class Class2;
class Class1
{
   Class2* pC1;
   public :
      void baz(Class2*);
};

#endif

0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
There are three different scenarios.  There are probably more, but these three are the ones that come of the top of my head.

Scenario 1:
You're using class2 in the declaration of class1 and you're using class1 in the declaration of class2.

Scenario 2:
You're using class2 in the declaration of class1 and you're using class1 in the implementation of class2.

Scenario 3:
You're using class2 in the implementation of class1 and you're using class1 in the implementation of class2.

KangaRoo gave you an example of Scenario 1.  In Scenario 1 both classes have to be used as pointers within each other.

Continue ....
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
Scenario 2:
In scenario 2 you can use non-pointer variables.
Example:
//Code for foo.h
#ifndef MY_FOO_H_FILE
#define MY_FOO_H_FILE

class foo
{
public:
     foo(int y);
     int x;
};

#endif //MY_FOO_H_FILE

//**********************************
//Code for foo.cpp
#include "foo2.h"

foo::foo(int y)
{
     foo2 pSomeFoo(8);
     x=pSomeFoo.x;
}

//**********************************
//Code for foo2.h
#ifndef MY_FOO2_H_FILE
#define MY_FOO2_H_FILE
#include "foo.h"

class foo2
{
public:
     foo2(int y){foo a(9);x=a.x;};
     int x;
};

#endif //MY_FOO2_H_FILE

//**********************************
//The main code MyApp.cpp
#include "foo2.h"

int main(int argc, char* argv[])
{
     foo myfoo(9);
     foo2 Myfoo2(3);
     myfoo.x = Myfoo2.x;
     return 0;
}

0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
Notice that in Scenario 2, no forward declaration was needed.
The foo2 class can use foo in it's declaration file, and it can use it as a non-pointer.

Because foo only needs to use foo2 in the implementation (*.cpp file), it does not have to have {#include "foo2.h"} in the header file.  It can be put in the *.cpp file instead.
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
Scenario 3:
In scenario 3 you can also use non-pointer variables.
This method requires you to move both "#include" in to the *.cpp file.
The classes can not contain each others code in the header file.  They can only reference each other in the *.cpp file.

For an example, look at the above foo class.
0
 

Expert Comment

by:SrinivasaRao
Comment Utility
I think you can use friend keyword. make a class as friend in two directions. If its not working do comeback.
bsr
0
 

Expert Comment

by:SrinivasaRao
Comment Utility
I think you can use friend keyword. make a class as friend in two directions. If its not working do comeback.
bsr
0
 
LVL 30

Accepted Solution

by:
Axter earned 50 total points
Comment Utility
I just realized, I gave you bad info on Scenario 1.
Only class has to use pointers.  The other class can use non-pointers.
Example:
//**********************************
//Code for foo.h
#ifndef MY_FOO_H_FILE
#define MY_FOO_H_FILE

class foo2;

class foo
{
public:
     foo(foo2 *pMyFoo2);
     foo(int y){x=y;};
     int x;
};

#endif //MY_FOO_H_FILE

//**********************************
//Code for foo.cpp
#include "foo2.h"

foo::foo(foo2 *pMyFoo2)
{
     x=pMyFoo2->x;
}

//**********************************
//Code for foo2.h
#ifndef MY_FOO2_H_FILE
#define MY_FOO2_H_FILE
#include "foo.h"

class foo2
{
public:
     foo2(foo MyFoo){x=MyFoo.x;};
     int x;
};

#endif //MY_FOO2_H_FILE



//**********************************
//The main code MyApp.cpp
#include "foo2.h"
int main(int argc, char* argv[])
{
     foo myfoo(9);
     foo2 Myfoo2(3);
     myfoo.x = Myfoo2.x;
     foo my_other_foo(&Myfoo2);
     return 0;
}


0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
Scenario 1:
So if you set up your code right, at most, you should only have to make one class use pointers to reference the other class.
The second class should be able to use non-pointers.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:newborn
Comment Utility
many many thanx to all:)

Axter u have the points just clear one thing,i am writing a socket app when i include both windows.h and winsock2.h in one app it gives me almost 66 redefinition errors, i am not sure about the problem,is it file inclusion problem or what, that is why i am asking about the header guards sort of thing, but they cannt solve this prob, if i exclude windows.h all gets well,dilemma why?
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
Can you post your code.

Regardless of how your header file is setup, you should always put header guards on it.
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
I misunderstood your last comment.

Where do you have windows.h and winsock2.h at.

Is it in the header or in the *.cpp file?
0
 

Author Comment

by:newborn
Comment Utility
windows.h in header of app file and winsock2.h in header of MySocket class
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
Try moving the headers into the *.cpp files.
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
Oops!!! Type-O
Try moving the #include into the *.cpp files.
0
 

Author Comment

by:newborn
Comment Utility
cannot do that the header file contains wsastuff like

WSADATA WSAData;
+++++++++++

this stuff needs winsock2.h
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
Can you post the header files that have the conflict?
0
 

Author Comment

by:newborn
Comment Utility
i was playing around with the placement of header files

# include <winsock2.h>
# include <windows.h>

is allright

# include <windows.h>
# include <winsock2.h>

is erroneous

error example

c:\program files\microsoft visual studio\vc98\include\winsock2.h(99) : error C2011: 'fd_set' : 'struct' type redefinition

code is allright, it works fine,i am stuck only with the header file placemen
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
So why don't you keep them in the order in which you don't get an error?
0
 

Author Comment

by:newborn
Comment Utility
i need reason,i wasted 4 days on this stupid problem,why  windows program doesnt complain about the absence of windows.h,any way i am PAQing the question
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now