包含标头时,'operator Apple'不是可识别的运算符或类型
'operator Apple' is not a recognized operator or type when including a header
我会简短的。
我有 2 个类:Apple
和 Orange
,如下所示:
Apple.h
(Apple.c
为空白(
#ifndef APPLE_H_
#define APPLE_H_
class Apple {};
#endif /* APPLE_H_ */
Orange.h
:
#ifndef ORANGE_H_
#define ORANGE_H_
#include "Apple.h"
class Orange {
public:
Orange();
virtual ~Orange();
operator Apple ();
};
#endif /* ORANGE_H_ */
Orange.cpp
:
#include "Orange.h"
Orange::Orange() {
// TODO Auto-generated constructor stub
}
Orange::~Orange() {
// TODO Auto-generated destructor stub
}
Orange::operator Apple() {
Apple y;
return y;
}
因为这些,这些都很棒。
但是当我#include "Orange.h"
添加到Apple.h
时,我得到'operator Apple' is not a recognized operator or type
错误。
如下:
#ifndef APPLE_H_
#define APPLE_H_
#include "Orange.h"
class Apple {};
#endif /* APPLE_H_ */
#include "Orange.h"
制造的问题是什么?
这是因为你现在有一个循环依赖:Orange.h
取决于Apple.h
,这取决于Orange.h
等。
在Orange.h
头文件中,声明 Apple
类可能就足够了:
// Tell the compiler that there is a class named `Apple`
class Apple;
class Orange {
public:
Orange();
virtual ~Orange();
operator Apple ();
};
然后在Orange.cpp
源文件中包括Apple.h
头文件。
相关文章:
- std::visit无法识别类型
- 我有两个类需要在同一 cpp 文件中相互引用,但第一个类无法识别第二个类类型的对象
- 我的头文件 DNode 中的我的数据类型未被识别为数据类型
- 无法在VSCode中设置C++调试;无法识别调试类型
- C++ 编译错误:gnu_printf是无法识别的格式函数类型
- C++将 wwn 字符串转换为识别为十六进制的数据类型
- 编写完整专业化以识别void类型的问题
- 无法识别协变返回类型
- 我该如何允许类型的对象非专业化并在不使用指针的情况下识别何时是这种情况
- 为什么模板<类型名...>不能通过模板<模板类型名>识别为可实例化<typename>?
- CGAL 无法识别类型
- 为什么我的Visual Studio 2017编译器无法识别C++17类型推导指南
- 当例外处理时,类型识别如何工作
- 无法识别初始化类型
- 从基类识别派生的类类型
- ActiveMQCPP - 识别测量类型
- 运算符 + 无法识别类型
- 如何在增强融合向量中识别类型
- 可识别类型转换的数字文字
- 在C++中以通用的可移植方式识别类型