1

I have the following c++ classes:

Page.h:

#ifndef PAGE_H_
#define PAGE_H_

#include "Process.h"

class Page {
public:
    Page();
    virtual ~Page();
    Process *process;
};

#endif /* PAGE_H_ */

and Process.h:

#ifndef PROCESS_H_
#define PROCESS_H_

#include <vector>
#include "Page.h"

class Process {
public:
    Process();
    virtual ~Process();

    int size;
    double life_remaining;
    std::vector<Page> pages;
};

#endif /* PROCESS_H_ */

When I compile I get the following error:

../src/Process.h:21:14: error: ‘Page’ was not declared in this scope
../src/Process.h:21:18: error: template argument 1 is invalid
../src/Process.h:21:18: error: template argument 2 is invalid

How do I correct this? When I comment out the lines: #include "Proccess.h" and Process *process; then it compiles. When I remove the comments it gives me the error

user000001
  • 32,226
  • 12
  • 81
  • 108

3 Answers3

4

Use a forward declaration instead of inclusion in Page.h:

//replace this:
//#include "Process.h"

//with this:
class Process;

class Page {
public:
    Page();
    virtual ~Page();
    Process *process;
};
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
2

You have a circular dependency between Process and Page.

Instead of...

#include "Process.h"

...in Page.h , forward declare...

class Process;

...and this will allow you to have Process* process; in your Page class.

johnsyweb
  • 136,902
  • 23
  • 188
  • 247
0

I think that Process.h is first included in Page.h rather than Page.h in Process.h.

mikithskegg
  • 806
  • 6
  • 10