Problem with circular dependicies

Feb 6, 2019 at 6:18pm
Hello guys.I am trying to resolve one problem but i can'n.I have two classes:
class FileSystemElement and class Folder.Class FileSystemElement contains a pointer to a Folder(*Folder) and Folder contains array of FileSystemElement pointers.And also Folder is inherited from FileSystemElement,but it can't see it as a base class.I tried to put in one header file both headers but it didn't resolve a problem.

FileSystemElement.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 #pragma once
#include<string>
#include"Folder.h"
class Folder;

class FileSystemElement
{protected:
	char *ime;
	char *ekstenzija;
	Folder *p;
public:
	FileSystemElement() :p(nullptr), ime(nullptr), ekstenzija(nullptr){}
		FileSystemElement(char * ime,char *ekstenzija);
	void Set_folder();
	virtual ~FileSystemElement();
	virtual char * Vrati() = 0;
	virtual void PrintList() = 0;

};

Folder.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#pragma once 
#include<iostream>
#include"FileSystemElement.h"
class FileSystemElement;

using namespace std;

class Folder:public FileSystemElement
{
private:
	FileSystemElement **m;
public:
	Folder() :FileSystemElement()
	{
		m = new FileSystemElement*[2018];
		for (int i = 0; i < 2018; i++)
			m[i] = nullptr;
	}
	virtual ~Folder();
	virtual char *Vrati();
	virtual void PrintList();




};

2 Errors in Folder.h
FileSystemElement not base of Folder or a member
Last edited on Feb 6, 2019 at 6:29pm
Feb 6, 2019 at 7:01pm
In folder.h, there is no point in forward declaring your FileSystemElement class if you include the file which contains its definition.

You're doing the same with a forward declaration of the Folder class in your FileSystemElement.h file.

Try removing the include in your FileSytstemElement file, and the forward declaration in your folder file
Feb 6, 2019 at 7:19pm
Thanks for your reply.
Feb 6, 2019 at 7:35pm
Did it fix your issue?
Feb 6, 2019 at 8:35pm
Yes,it has.It helped me fix some other stuff that didn't work.Thanks once more.
Feb 6, 2019 at 8:36pm
Cool. No worries, glad to have been of any help!
Feb 6, 2019 at 8:49pm
Short answer: in FileSystemElement.h, move #include "Folder.h" to the end of the file.

To see why this is isn't working, consider what happens when your program contains #include "FileSystemElement.h" :

- The preprocessor runs and sees #include "FileSystemElement.h"
- It starts processing FileSystemElement.h. At line 3 it's told to process "Folder.h"
- At line 3 of Folder.h, it's told to parse FileSystemElement.h" again. But [u]because FileSystemElement.h contains #pragma once and because you already started processing it,
that file is skipped.
- Moving along in Folder.h, it sees the forward declaration to FileSystemElement, but when you say that Folder is a public FileSystemElement, it doesn't know how big FileSystemElement is, so it generates an error.

By moving the #include "Folder.h" to the end of FileSystemElement.h, you ensure that FileSystemElement is defined by the time you declare Folder.
Feb 6, 2019 at 8:54pm
Damn, moving an include to the end of a file tickles my OCD so much, I don't know if I'd ever be able to do that...
Feb 6, 2019 at 9:24pm
Thanks for the answer,I would never come up to that solution.
Feb 7, 2019 at 2:52pm
moving an include to the end of a file tickles my OCD
I hear ya. The other possibility is to remove the #include "Folder.h" from FileSystemElement.h and require the pogrammer to include Folder.h themselves, but that's messy too.

Dave
Topic archived. No new replies allowed.