Solved

General C++ Question...

Posted on 1999-01-12
2
203 Views
Last Modified: 2010-04-02
Hello experts exchange

I have a C++ program like so:

header1.h
--------- cut
#ifndef FOO1H
#define FOO1H

#include "Foo2.h"
class Foo1
{ void Func (Foo2 *);
};

#endif

--------- end

header2.h
--------- cut
#ifndef FOO2H
#define FOO2H

#include "Foo1.h"
class Foo2
{ void Func (Foo1 *);
};

#endif
--------- end

--------- Then their respective cpp files with function definitions etc...

This is resulting in compiling errors, mainly "Syntax" errors.  Should this happen?  If so, how do I correct  it?  I can get around it by including (essentially) "Foo2.h" in "Foo1.cpp" and then taking a void * parameter instead, but this is messy.  I want a good solution.
0
Comment
Question by:IgorStravinsky
2 Comments
 

Accepted Solution

by:
mozack earned 50 total points
ID: 1181970
You have a circular dependency. You cannot have 2 header files include each other. This can be avoided by using a forward declaration.  i.e.

// header1.h
class Foo2;  // the actual definition will come in header2.h
             // notice there is no #include here
class Foo1
{
   void Func(Foo2*);
};
0
 

Author Comment

by:IgorStravinsky
ID: 1181971
Thanks, that has corrected my problem . I knew there was a way to do it!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

829 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