[英]C++ multiple inheritance, multiple definition when there isn't?
I'm implementing hardware drivers for an embedded C/C++ project, and trying to make things a bit more flexible for future projects. 我正在为嵌入式C / C ++项目实现硬件驱动程序,并试图使将来的项目变得更加灵活。
I have the vast majority of the work done in LCD.hpp/LCD.cpp, where there's a class that has five virtual functions. 我在LCD.hpp / LCD.cpp中完成了绝大多数工作,其中有一类具有五个虚拟功能。 Four of these are for twiddling GPIO pins and sending SPI messages, and the fifth is for implementing various fonts.
其中四个用于旋转GPIO引脚并发送SPI消息,第五个用于实现各种字体。 A shortened class declaration is as follows:
缩短的类声明如下:
//LCD.hpp
#include <cstdint>
#ifndef LCD_HPP
#define LCD_HPP
class LCD {
public:
virtual void write_character(char what) = 0; //Stores data into a buffer in LCD through another function
protected:
virtual void SPI_TX(uint8_t *TXData, uint8_t length, bool ToBeContinued) = 0;
virtual void update_RST(bool pinstate) = 0;
virtual void update_DC(bool pinstate) = 0;
virtual void update_backlight(uint8_t brightness) = 0;
};
#endif
Moving on, I implemented a font-printing write_character as such. 继续,我实现了这样的字体打印write_character。
//LCD_FixedWidth.hpp
#include <cstdint>
#include "LCD.hpp"
#ifndef LCD_FIXEDWIDTH_HPP
#define LCD_FIXEDWIDTH_HPP
class LCD_FixedWidth : virtual public LCD {
public:
void write_character(char what);
};
#endif
Now it's time for the various hardware bits. 现在是时候使用各种硬件了。
//LCD_hardware.hpp
#include <cstdint>
#include "LCD.hpp"
#include "LCD_FixedWidth.hpp"
#ifndef LCD_HARDWARE_HPP
#define LCD_HARDWARE_HPP
class LCD_hardware : virtual public LCD {
protected:
void SPI_TX(uint8_t *TXData, uint8_t length, bool ToBeContinued);
void update_RST(bool pinstate);
void update_DC(bool pinstate);
void update_backlight(uint8_t brightness);
};
And then a class to tie it all together, still in LCD_hardware.hpp... 然后是一个类,将所有内容捆绑在一起,仍然在LCD_hardware.hpp ...
class LCD_meta : public LCD_hardware, public LCD_FixedWidth {
public:
void write_character(char what) { LCD_FixedWidth::write_character(what); };
protected:
void SPI_TX(uint8_t *TXData, uint8_t length, bool ToBeContinued) { LCD_hardware::SPI_TX(TXData, length, ToBeContinued); };
void update_RST(bool pinstate) { LCD_hardware::update_RST(pinstate); };
void update_DC(bool pinstate) { LCD_hardware::update_DC(pinstate); };
void update_backlight(uint8_t brightness) { LCD_hardware::update_backlight(brightness); };
};
#endif
And for all of this, I get a multiple definition of LCD_FixedWidth::write_character(char) error. 对于所有这些,我得到了LCD_FixedWidth :: write_character(char)错误的多个定义。 Anybody see anything I'm missing here?
有人看到我在这里想念的东西吗? All of my headers are guarded properly, and I can only see one implementation of write_character...
我所有的标头都受到适当的保护,我只能看到一种write_character实现。
This was caused by having LCD_meta and LCD_hardware in the same header file. 这是由于在同一头文件中包含LCD_meta和LCD_hardware引起的。 The code for the LCD_hardware functions was in an implementation file, so the LCD_meta class didn't actually have those functions defined yet... One class per file!
LCD_hardware函数的代码在实现文件中,因此LCD_meta类实际上尚未定义那些函数...每个文件一个类!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.