我有三个班级:人,家庭和图。人与家庭之间存在周期性依赖关系,因此我在家庭中转发已声明的人。它们都包含在Graph中。编译时,我得到一个g ++错误,指出(对于graph.cpp中Graph :: *的每个实例) “'Graph'不能命名类型”,并且graph.h中的所有数据成员都表示未在此范围内声明它们。 (他们都是公开的)
问题在于修复编译器错误并了解如何处理此循环继承依赖关系,以便对Graph类进行编译。
我尝试过在Graph中声明Person和Family,以及一次使用#pragma。
家庭。h
#pragma once
//...
class Family{
int foo;
};
Family.cpp
#include "person.h"
//...
void Family::functions(){}
Person.h
#pragma once
#include "family.h"
//...
class Person{
int foo;
};
Person.cpp
#include "family.h"
void Person::functions(){}
Graph.h
#pragma once
#ifndef GRAPH_H
#define GRAPH_H
#include "person.h"
#include "family.h"
class Graph{
Person * people[NUM_PEOPLE];
Family * families[NUM_FAMILY];
};
Graph.cpp
//...
class Family;
//...
Graph::Graph(){}
//Here Graph does not name a type
void Graph::GraphFunc1(int someParams){
//Here Graph does not name a type
//implementation...
//Lets say somewhere in this function we use people[] and families[]
people[0] = something;
families[1] = something;
//Here families and people are not defined in this scope
//Although they are declared PUBLICLY in Graph.h
}
我希望对象指针人员和家庭的数组在graph.cpp的范围内定义,因为它们是在其标题中公开定义的。这与Graph不命名类型有关吗? FWIW,“图形未命名类型”编译器错误先于其他错误出现。