错误:“页面”未在此范围内声明

error: ‘Page’ was not declared in this scope

本文关键字:范围内 声明 页面 错误      更新时间:2023-10-16

我有以下 c++ 类:

页.h:

#ifndef PAGE_H_
#define PAGE_H_
#include "Process.h"
class Page {
public:
    Page();
    virtual ~Page();
    Process *process;
};
#endif /* PAGE_H_ */

和进程.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"和"流程";然后编译。当我删除评论时,它给了我错误

使用前向声明而不是包含在Page.h中:

//replace this:
//#include "Process.h"
//with this:
class Process;
class Page {
public:
    Page();
    virtual ~Page();
    Process *process;
};

您在 ProcessPage 之间有一个循环依赖关系

而不是。。。

#include "Process.h"

。在Page.h中,向前声明...

class Process;

。这将使您在Page class中拥有Process* process;

我认为Process.h首先包含在Page.h中,而不是Process.h中的Page.h。