Solved

General C++ Question...

Posted on 1999-01-12
2
201 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Building cUrl in Windows v7.43.0 4 28
C Language combined operators 28 106
sorting efficency of sorting algorithm 30 103
object oriented programming comparison 5 55
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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 viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

896 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

16 Experts available now in Live!

Get 1:1 Help Now