2

I am trying to compile my code, but I am getting an error with classes. One of the classes compiled fine(Example.h), but the other(Name.h) keeps giving me the class does not name type error. I think it has something to do with circular dependency, but how do I fix that without a forward deceleration?

Name.h

#ifndef _NAME
#define _NAME
#include <iostream>
#include <string>
using namespace std;
#include "Example.h"
class Name{
};
#endif

Example.h

#ifndef _EXAMPLE
#define _EXAMPLE
#include <iostream>
#include <string>
using namespace std;
#include "Name.h"
class Example{

};
#endif

I saw a post about using forward deceleration, but I need to access memebers from the Example class..

user2351234
  • 965
  • 2
  • 12
  • 20
  • 1
    You cannot include `A.h` in `B.h` and `B.h` in `A.h`. It is circular dependency indeed. You can try forward declaration of classes. – Mikhail Mar 25 '14 at 07:14
  • But will forward declaration give me access to the other classes member functions? – user2351234 Mar 25 '14 at 07:15
  • Nothing to do with the question, but since you're (presumably) using modern C++ compiler, you can do away with the archaic `#ifndef _EXAMPLE #define _EXAMPLE #endif` thing. Just use `#pragma once`. (http://en.wikipedia.org/wiki/Pragma_once) – Colin Basnett Mar 25 '14 at 07:16
  • 1
    @user2351234 No, forward declaration of class `A` by its own only let you declare pointers to `A` in `B.h` file. But you can include whole declaration of `A` from `A.h` in `B.cpp`. – Mikhail Mar 25 '14 at 07:18
  • 2
    @cmbasnett: `#pragma once` is non-standard and not supported by all modern compilers so it's perfectly reasonable and correct to use portable `#ifndef / #define` include guards if only the name chosen were one allowed for use by user programs. – CB Bailey Mar 25 '14 at 07:27
  • If `Name` needs to access members from `Example`, why do you need to include `Name.h` in `Example.h`? – rozina Mar 25 '14 at 07:30
  • and don't forget to [stop using](http://stackoverflow.com/q/1452721/847349) `using namespace std;` in header files, and, better, in cpp too – Dmitry Ledentsov Mar 25 '14 at 07:53

3 Answers3

2

You have a circular dependency, where each header tries to include the other, which is impossible. The result is that one definition ends up before the other, and the name of the second is not available within the first.

Where possible, declare each class rather than including the entire header:

class Example; // not #include "Example.h"

You won't be able to do this if one class actually contains (or inherits from) another; but this will allow the name to be used in many declarations. Since it's impossible for both classes to contain the other, you will be able to do this (or maybe just remove the #include altogether) for at least one of them, which should break the circular dependency and fix the problem.

Also, don't use reserved names like _NAME, and don't pollute the global namespace with using namespace std;

Community
  • 1
  • 1
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
1

see, here you are including #include "Example.h" in Name.h and #include "Name.h" in Example.h. suppose compiler compiles Name.h file first so _NAME is defined now, then it tries to compile Example.h here compiler wants to include Name.h but the content of Name.h will not be included in Example.h since _NAME is already defined, hence class Name is not defined inside Example.h.

you can explicitly do forward declaration of class Name; inside Example.h

rajenpandit
  • 1,265
  • 1
  • 15
  • 21
0

Try this:

Name.h

#ifndef NAMEH
#define NAMEH
#include <iostream>
#include <string>
using namespace std;

//#include "Example.h"
class Example;

class Name{
};
#endif

Name.cpp

#include "Name.h"
#include "Example.h"
...

Example.h

#ifndef EXAMPLEH
#define EXAMPLEH
#include <iostream>
#include <string>
using namespace std;

//#include "Name.h"
class Name;

class Example{

};
#endif

Example.cpp

#include "Example.h"
#include "Name.h"
...
hich9n
  • 1,578
  • 2
  • 15
  • 32