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.
newbornAsked:
Who is Participating?
 
AxterConnect With a Mentor Commented:
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
 
AxterCommented:
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
 
KangaRooCommented:
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
AxterCommented:
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
 
AxterCommented:
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
 
AxterCommented:
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
 
AxterCommented:
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
 
SrinivasaRaoCommented:
I think you can use friend keyword. make a class as friend in two directions. If its not working do comeback.
bsr
0
 
SrinivasaRaoCommented:
I think you can use friend keyword. make a class as friend in two directions. If its not working do comeback.
bsr
0
 
AxterCommented:
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
 
newbornAuthor Commented:
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
 
AxterCommented:
Can you post your code.

Regardless of how your header file is setup, you should always put header guards on it.
0
 
AxterCommented:
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
 
newbornAuthor Commented:
windows.h in header of app file and winsock2.h in header of MySocket class
0
 
AxterCommented:
Try moving the headers into the *.cpp files.
0
 
AxterCommented:
Oops!!! Type-O
Try moving the #include into the *.cpp files.
0
 
newbornAuthor Commented:
cannot do that the header file contains wsastuff like

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

this stuff needs winsock2.h
0
 
AxterCommented:
Can you post the header files that have the conflict?
0
 
newbornAuthor Commented:
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
 
AxterCommented:
So why don't you keep them in the order in which you don't get an error?
0
 
newbornAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.