难以理解循环声明
Having trouble to understand circular declaration
嗨,我正在尝试创建一个工厂方法,它返回类a的派生类,我在理解循环声明时遇到了问题,希望你能帮我解决这个问题。
谢谢。
AChildOne.cpp
#include "AChildOne.h"
AChildOne.h
#ifndef ACHILDONE_H
#define ACHILDONE_H
#include "A.h"
class A_CHILD_ONE : public A {
};
#endif
A.cpp
#include "A.h"
void A::a(){
Factory::fact();
};
A.h
#ifndef A_H
#define A_H
#include "Factory.h"
class A {
public:
static void a();
};
#endif
Factory.cpp
#include "Factory.h"
A *Factory::fact(){
return new A_CHILD_ONE;
}
Factory.h
#ifndef FACTORY_H
#define FACTORY_H
#include "A.h"
#include "AChildOne.h"
class Factory {
public:
static A *fact();
};
#endif
编译错误
g++ A.cpp Factory.cpp AChildOne.cpp -o test
In file included from Factory.h:5:0,
from A.h:4,
from A.cpp:1:
AChildOne.h:5:30: error: expected class-name before ‘{’ token
class A_CHILD_ONE : public A {
^
In file included from A.h:4:0,
from A.cpp:1:
Factory.h:9:10: error: ‘A’ does not name a type
static A *fact();
^
A.cpp: In static member function ‘static void A::a()’:
A.cpp:4:2: error: ‘fact’ is not a member of ‘Factory’
Factory::fact();
^
In file included from A.h:4:0,
from AChildOne.h:3,
from AChildOne.cpp:1:
Factory.h:9:10: error: ‘A’ does not name a type
static A *fact();
^
在Factory.h
中,您尝试包含A.h
;在A.h
中,您尝试包含Factory.h
。
将Factory.h
包含到A.cpp
中并从A.h
中删除应该会有所帮助。
Factory
声明依赖于A
接口。A
声明不依赖于Factory
声明,但A
定义依赖于。
此外,Factory.h
不需要知道AChildOne.h
,但Factory.cpp
需要知道。因此,将#include AChildOne.h
移动到Factory.cpp
。
相关文章:
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- 在基于范围的for循环中使用结构化绑定声明
- C++声明双链表,使用两个 for 循环双向遍历列表并打印
- 如何在头文件中声明类模板(由于循环依赖关系)
- 在变量声明中使用 for 循环
- 在不同循环中多次声明变量的优点
- C++循环依赖关系,未声明的标识符
- 如何每次在不同大小的循环中重新声明相同的数组
- C++ 中的类之间的数据重新循环 - 错误:'<class name>'未在此范围内声明
- 循环包含依赖项/转发声明
- 如何在 for 循环中声明多个名称相似的数组?
- 使用循环声明 Mat 类
- 循环依赖结构,使用前向声明时结构的错误重定义
- 如何在需要提及需要循环声明的其他类的类中初始化变量?
- 循环内的函数声明
- 如何在 for 循环中声明和使用结构数组?
- 难以理解循环声明
- C++常量循环声明
- 性能:循环声明与循环重新初始化
- 基于范围的循环声明必须声明变量