首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从外部定义的类继承时,不完全类型类错误的使用无效

从外部定义的类继承时,不完全类型类错误的使用无效
EN

Stack Overflow用户
提问于 2016-10-06 12:50:07
回答 1查看 1.2K关注 1票数 1

我有以下文件夹结构:

代码语言:javascript
运行
复制
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中有一个类:

代码语言:javascript
运行
复制
#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,并且应该包含在任何应该使用该类的内容中。它的内容如下:

代码语言:javascript
运行
复制
#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

代码语言:javascript
运行
复制
#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

现在,在编译时,我得到以下错误:

代码语言:javascript
运行
复制
../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.

是什么导致了这一切?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-06 12:52:53

要能够从类继承,您需要完整的定义,而不仅仅是前向声明。您需要包含rs485_device.h头文件。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39896493

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档