我有以下c ++类:
Page.h:
#ifndef PAGE_H_
#define PAGE_H_
#include "Process.h"
class Page {
public:
Page();
virtual ~Page();
Process *process;
};
#endif /* PAGE_H_ */
和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_ */
编译时出现以下错误:
../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
我该如何纠正? 当我注释掉这些行:#include“Proccess.h”和Process *过程;然后它编译。当我删除评论时,它会给我错误
答案 0 :(得分:4)
使用转发声明,而不是包含在Page.h
:
//replace this:
//#include "Process.h"
//with this:
class Process;
class Page {
public:
Page();
virtual ~Page();
Process *process;
};
答案 1 :(得分:2)
Process
和Page
之间有circular dependency。
而不是......
#include "Process.h"
...在Page.h中,转发声明......
class Process;
...这样您就可以在Process* process;
Page
中拥有class
。
答案 2 :(得分:0)
我认为Process.h首先包含在Page.h中而不是Process.h中的Page.h中。