gpt4 book ai didi

c++ - 从外部定义的类继承时无效使用不完整类型类错误

转载 作者:行者123 更新时间:2023-11-30 01:42:14 25 4
gpt4 key购买 nike

我有以下文件夹结构:

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++ - 从外部定义的类继承时无效使用不完整类型类错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39896493/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com