Solved

inlude file problem

Posted on 2001-06-15
21
458 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
ID: 6197599
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
ID: 6197616
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
ID: 6197629
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 30

Expert Comment

by:Axter
ID: 6197631
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
ID: 6197635
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
ID: 6197640
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
ID: 6197649
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
ID: 6197650
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
ID: 6197651
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
ID: 6197658
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
 

Author Comment

by:newborn
ID: 6197723
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
ID: 6197725
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
ID: 6197726
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
ID: 6197738
windows.h in header of app file and winsock2.h in header of MySocket class
0
 
LVL 30

Expert Comment

by:Axter
ID: 6197742
Try moving the headers into the *.cpp files.
0
 
LVL 30

Expert Comment

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

Author Comment

by:newborn
ID: 6197748
cannot do that the header file contains wsastuff like

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

this stuff needs winsock2.h
0
 
LVL 30

Expert Comment

by:Axter
ID: 6197752
Can you post the header files that have the conflict?
0
 

Author Comment

by:newborn
ID: 6197780
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
ID: 6198455
So why don't you keep them in the order in which you don't get an error?
0
 

Author Comment

by:newborn
ID: 6199127
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

821 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