我有以下文件夹结构:
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.
可能导致这种情况的原因是什么?
答案 0 :(得分:4)
为了能够从类继承,您需要完整的定义,而不仅仅是前向声明。您需要包含rs485_device.h
标头文件。