need help with mutual dependent class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// file directory.h
#ifndef ENTRY_H
#define ENTRY_H
#include<string>
#include<vector>
class Directory;
class Entry {
       public: Directory* parent;
               long created;
               long lastAccess;
               long lastUpdated;
                string name;
                vector<Entry*> contents;
               Entry(string n, Directory* p);
               virtual string getname();
};
#endif 






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
27
28
29
30
31
32
// file directory.cpp
#include"directory.h"
#include<vector>
#include<string>
#include"Entry.h"
using namespace std;

      Directory::Directory(string n, Directory* p):Entry(n, p) {
      }

      vector<Entry*> Directory:: getcontent() {
              return contents;
      }

      Entry* Directory::getChild(string n) {
                  for (int i = 0; i < contents.size(); ++i) {
                           Entry* c = contents[i];
                           if (c->getname() == n) {
                                   Directory* d = dynamic_cast<Directory*> (c);
                                   if (d != NULL)
                                           return d;
                                  /* File* da = dynamic_cast<File*> c;
                                   if (da != NULL)
                                           return da;*/
                           }
                  }
                  return NULL;
      }

      void Directory::add(Entry* c) {
          contents.push_back(c);
      }



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// file entry.h
#ifndef ENTRY_H
#define ENTRY_H
#include <vector>
#include <string>
class Directory; 
class Entry { public: 
Directory* parent; 
long created; 
long lastAccess; 
long lastUpdated;
 string name; 
vector<Entry*> contents; 
Entry(string n, Directory* p); 
virtual string getname(); }; 
#endif  



1
2
3
4
5
6
7
8
9
10
11
12
13
14
// file entry.cpp
#include"Entry.h"
#include<string>
#include"directory.h"
using namespace std;
       Entry::Entry(string n, Directory* p) {
              name = n;
              parent = p;
       }
       string Entry::getname() {
           return name;
 

      }

has these kinds of error " ‘Directory’ has not been declared
Entry::Entry(string n, Directory* p) {"


how to make these code work ? thank you :)
Last edited on
First, please use code tags when posting code. See http://www.cplusplus.com/articles/jEywvCM9/

Why your directory.h and entry.h are identical?
just like that every file or directory has some meta data for name, string lastedited time, so I make it an entry
The contents of
// file directory.h
and
// file entry.h
ARE IDENTICAL

Your directory.h is nothing but an exact copy of entry.h
You don't define class Directory anywhere.

If you have posted what you have, then it is obvious why you have errors.
Topic archived. No new replies allowed.