-3

I have three classes.

first class:

#ifndef C_LINKED_LIST_H
#define C_LINKED_LIST_H

class CLinkedList {
private:
//removed code for brevity

public:
// removed code for brevity
};

#endif

second class:

#ifndef C_SSF_FOLDER_CONTAINER_H
#define C_SSF_FOLDER_CONTAINER_H

#include "C_SSF_Folder.h"
#include "CLinkedList.h"

class C_SSF_Folder_Container {
private:
    // removed code for brevity

public:
    int Add_Folder(C_SSF_Folder *_pcl_SSF_Folder);
    C_SSF_Folder *Get_Folder(int _i_Index);
    C_SSF_Folder *Get_Folder(char *_pch_Name);
        //^-----errors
};

#endif C_SSF_FOLDER_CONTAINER_H

my third class #ifndef C_SSF_FOLDER_H #define C_SSF_FOLDER_H

#include <windows.h>
#include <fstream>
#include "C_SSF_Folder_Container.h"

using namespace std;

class C_SSF_Folder {
public:

private:
    C_SSF_Folder_Container cl_SSFFC_Folder_Container;

public:

};

#endif

my third class C_SSF_Folder. I am including "C_SSF_Folder_Container.h" and declaring a C_SSF_Folder_Container container. Before declaring the variable it compiles fine. After I declare it I get syntax errors in my C_SSF_Folder_Container Severity Code Description Project File Line Suppression State Error C2061 syntax error: identifier 'C_SSF_Folder' CSSFileSystem\projects\cssfilesystem\cssfilesystem\c_ssf_folder_container.h 16
Error C2061 syntax error: identifier 'C_SSF_Folder' CSSFileSystem \projects\cssfilesystem\cssfilesystem\c_ssf_folder_container.h 19
As I myself look into it I think there is a problem because my C_SSF_Folder is including C_SSF_Folder_Container. and C_SSF_Folder_Container is including C_SSF_Folder but the defines should take care of it? Other than that I have no clue what's the problem.

Everything is typed correctly.

  • 1
    Welcome to Stack Overflow. Please take the time to read [The Tour](http://stackoverflow.com/tour) and refer to the material from the [Help Center](http://stackoverflow.com/help/asking) what and how you can ask here. – πάντα ῥεῖ Feb 04 '17 at 02:28
  • Possible duplicate of [Resolve header include circular dependencies](http://stackoverflow.com/questions/625799/resolve-header-include-circular-dependencies) – πάντα ῥεῖ Feb 04 '17 at 03:46
  • Thank you so much. I got it to work! – shakespeare Feb 04 '17 at 07:09
  • I put a forward declaration of the class in my header file. But that gave me problems when I tried to use functions from the class defined variable. So looking through the web I realized I needed to declare the functions that I was calling. That sent me into a whirlpool messing around with all kind of weird stuff. At the end after I declared the class in the header I included the header file of that class In the .cpp. This worked. Is this the correct way of doing it? Or am I using a bad habbit solution? – shakespeare Feb 04 '17 at 07:11
  • Is it normal to have a .cpp that declares a class have other includes in the .cpp file besides it's .h file? – shakespeare Feb 04 '17 at 07:19
  • Yes thar's totally normal. – πάντα ῥεῖ Feb 04 '17 at 07:43
  • Not only it's normal, it's even considered good (best) practice to forward declare what you need in the header files, and include the final declarations in your translation unit. – πάντα ῥεῖ Feb 04 '17 at 08:01

2 Answers2

2

You've got a circular #include -- C_SSF_Folder_Container.h #includes C_SSF_Folder.h and C_SSF_Folder.h #includes C_SSF_Folder_Container.h.

This would cause an infinite regress (and a compiler crash) except that you've got the #ifndef/#define guards at the top of your files (as you should); and because of them, instead what you get is that one of those two .h files can't see the other one, and that's why you get those errors.

The only way to fix the problem is to break the circle by deleting one of the two #includes that comprise it. I suggest deleting the #include "C_SSF_Folder.h" from C_SSF_Folder_Container.h and using a forward declaration (e.g. class C_SSF_Folder; instead.

Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
  • some compilers do have their own guarding , without ifdefs, but circular headers cause unexpected behavior. I never saw any compiler crash, but it could starts to give strange errors, like unknown identifier and such – Swift - Friday Pie Feb 04 '17 at 02:58
0

C_SSF_Folder.h and C_SSD_Folder_Container.h are including each other(Circular Dependency).

When the compiler compiles C_SSF_Folder_Container object, it needs to create a C_SSF_Folder object as its field, however, the compiler needs to know the size of C_SSF_Folder object, so it reaches C_SSF_Folder object and tries to construct it. Here is the problem, when the compiler is constructing C_SSF_Folder object, the object has a C_SSF_Folder_Container object as its field, which is a typical chicken and egg question, both files depends on each other in order to compile.

So the correct way to do it is to use a forward declaration to break the circular dependency(including each other).

In your C_SSF_Folder.h, make a forward declaration of C_SSF_Folder_Container.

#include <windows.h>
#include <fstream>

using namespace std;

class C_SSF_Folder_Container;

class C_SSF_Folder {
public:

private:
    C_SSF_Folder_Container cl_SSFFC_Folder_Container;

public:

};

#endif

Finally, include C_SSF_Folder_Container.h in your C_SSF_Folder.cpp.


You can also learn more in the following links:

Circular Dependency (Wiki):
https://en.wikipedia.org/wiki/Circular_dependency
Forward Declaration by Scott Langham
What are forward declarations in C++?
Community
  • 1
  • 1
Zhou Zhi Hua
  • 371
  • 1
  • 10
  • Thank you. I put a forward declaration of the class in my header file. But that gave me problems when I tried to use functions from the class defined variable. So looking through the web I realized I needed to declare the functions that I was calling. That sent me into a whirlpool messing around with all kind of weird stuff. At the end after I declared the class in the header I included the header file of that class In the .cpp. This worked. Is this the correct way of doing it? Or am I using a bad habbit solution? – shakespeare Feb 04 '17 at 07:09
  • Is it normal to have a .cpp that declares a class have other includes in the .cpp file besides it's .h file? – shakespeare Feb 04 '17 at 07:19
  • @ShakeSpear I'm not so pro so I don't know if it is a bad practice, but I think it is perfectly fine for cpp to include, cpp files can include their own headers so why not other header files? By the way, if you do not include C_SSC_Folder_Container.h in C_SSC_Folder.cpp, it is going to give you a lot of error because you forward declared class C_SSC_Folder_Container in the header file, but you did not provide the definition of it. – Zhou Zhi Hua Feb 04 '17 at 07:25
  • Refers to πάντα ῥεῖ 's comment, it is ok for cpp file to include other file header – Zhou Zhi Hua Feb 04 '17 at 07:31
  • Great. Thanks. Much appreciated! – shakespeare Feb 04 '17 at 07:36