从外部定义的类继承时,使用不完整类型类错误无效
Invalid use of incomplete type class error when inheriting from class defined externally
文件夹结构如下:
src/
├── drivers
│ ├── drv_rs485_bus.h
│ └── rs485_bus
│ ├── rs485_device.cpp
│ └── rs485_device.h
└── modules
└── dx_servos
├── CMakeLists.txt
├── dx_servo.cpp
└── dx_servo.h
在rs485_device.h
中是一个类:
#pragma once
#include <cstdint>
#include <memory>
#include "rs485_bus.h"
namespace rs485 {
class Rs485Device {
public:
explicit Rs485Device(std::shared_ptr<rs485::Rs485Bus> bus, uint8_t address);
~Rs485Device();
private:
std::shared_ptr<rs485::Rs485Bus> _bus;
uint8_t _address;
};
} // ns: rs485
drv_rs485_bus.h
暴露了类Rs485Device
,并且应该包含在应该使用该类的任何内容中。它有以下内容:
#pragma once
#include <cstdint>
#include <memory>
namespace rs485 {
class Rs485Bus;
class Rs485Device;
extern std::shared_ptr<Rs485Bus>
get_bus();
extern bool
get_open();
}
现在,我想创建一个类DxServo
,它继承Rs485Device
。我包括drv_rs485_bus.h
,并像这样声明DxServo
:
#pragma once
#include <drivers/drv_rs485_bus.h>
#include <cstdint>
#include <memory>
#include <string>
namespace dx_servo {
class DxServo : public rs485::Rs485Device {
public:
explicit DxServo(const std::string name, uint16_t address,
std::shared_ptr<rs485::Rs485Bus> bus);
~DxServo();
private:
uint16_t _address;
std::shared_ptr<rs485::Rs485Bus> _bus;
};
} // ns: dx_servo
现在,在编译时,我得到以下错误:
../src/modules/dx_servos/dx_servo.h:12:31: error: invalid use of incomplete type ‘class rs485::Rs485Device’
class DxServo : public rs485::Rs485Device {
^
compilation terminated due to -Wfatal-errors.
是什么原因导致的?
为了能够从一个类继承,您需要完整的定义,而不仅仅是一个前向声明。您需要包含rs485_device.h
头文件。
相关文章:
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 类中的字符串不命名类型
- 不完整类型错误(E0409、E0070、E0515)
- CRTP 单一实例不完整类型或非文本类型
- 使用具有结构不完整类型错误的模板
- 哪些存储了不完整类型的 STL 数据结构可以用作类成员?
- Xalloc 不命名类型
- (结构名/类名)不命名类型C++
- 是否允许类类型的 std::function 成员变量(不完整类型)?
- MSVC 在不知道类型的情况下评估上下文(和错误)
- 为什么我得到变量"Node"不是类型名称错误
- Visual Studio Code "variable " u8 的 C/C++ 扩展名 " " 不是类型名称"
- 将"std::string {aka std::basic_string}"赋值中的不兼容类型<char>
- 实例化具有不完整类型的类模板格式不正确(如果该类型是在之后定义的)
- C++ 外部类不命名类型
- 有没有办法用不变语法获得当前的类类型?
- 标准容器模板可以用不完整类型实例化吗?
- 什么时候不能用“类”代替“类型名”
- 用Clang编译c++11代码的不完整类型
- 为什么C++不能用 "super class" 类型的右值初始化 "derived class" 类型的变量?