Link to home
Start Free TrialLog in
Avatar of newborn
newborn

asked on

inlude file problem

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.
Avatar of Axter
Axter
Flag of United States of America image

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
Avatar of KangaRoo
KangaRoo

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

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 ....
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;
}

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.
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.
I think you can use friend keyword. make a class as friend in two directions. If its not working do comeback.
bsr
I think you can use friend keyword. make a class as friend in two directions. If its not working do comeback.
bsr
ASKER CERTIFIED SOLUTION
Avatar of Axter
Axter
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of newborn

ASKER

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?
Can you post your code.

Regardless of how your header file is setup, you should always put header guards on it.
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?
Avatar of newborn

ASKER

windows.h in header of app file and winsock2.h in header of MySocket class
Try moving the headers into the *.cpp files.
Oops!!! Type-O
Try moving the #include into the *.cpp files.
Avatar of newborn

ASKER

cannot do that the header file contains wsastuff like

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

this stuff needs winsock2.h
Can you post the header files that have the conflict?
Avatar of newborn

ASKER

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
So why don't you keep them in the order in which you don't get an error?
Avatar of newborn

ASKER

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