I am writing on QT 5.10. There is a certain exchange protocol for data transmission. Each message contains a header and message body. This protocol encapsulates another protocol, which also contains its own header and data, this internal protocol contains many different types of blocks, the data of which is collected and analyzed in a unique way. What kind of message - can be determined by two fields in the header, and the method of processing the third field. Inheritance suggests itself: the base class of the external protocol <-the base class of the internal protocol <-sub class of a specific message. There were no problems. In order not to write giant switch ... case ... (still hundreds of message options) - I decided to create an associative array of data block handler objects. Each handler must be present in one instance and create a controller class in the constructor of which the entire associative array of objects will be created, and the required handler will be called by a unique key extracted from the block header with its own virtual methods. Here a problem arose: a compilation error occurs on a pointer to the base class in an associative array:
D:\MyPrograms\RapidVHF\RapidVHF\frameselector.h:19: ошибка: 'RAP1' was not declared in this scope map<uint32_t, RAP1*> framemap; ^ Here is the code snippet:
#ifndef FRAMESELECTOR_H #define FRAMESELECTOR_H #include <stdint.h> #include <map> #include <memory> //#include "ui_mainwindow.h" #include "usertypes.h" #include "rap1.h" //#include "datatomodem.h" //#include "pskotasetframe.h" using namespace std; class FrameSelector { private: map<uint32_t, unique_ptr<RAP1> > framemap; //ошибка возникает и здесь //map<uint32_t, RAP1*> framemap; // и здесь public: FrameSelector(Ui::MainWindow *UI); ~FrameSelector(); bool MakeFrame(uint8_t *pFrame = nullptr); bool CheckFrame(uint8_t *pFrame = nullptr); }; #endif // FRAMESELECTOR_H The actual description of the base class of the internal protocol
#ifndef RAP1_H #define RAP1_H #include <stdint.h> #include <QString> #include <QComboBox> #include "ripc.h" #include "usertypes.h" #include "ui_mainwindow.h" class RAP1 : public RIPC { protected: static uint8_t SequenceNumber; const int shGroupID = 20; const int shMessageID = 22; const int shMsgSequenceNum = 24; const int shMsgType = 25; const int shData = 26; public: RAP1(Ui::MainWindow *UI); virtual ~RAP1(); bool FillHeader(uint8_t* pFrame); QString toTakeDigitDataFromComboBox(QComboBox *pComboBox); virtual bool FillFrame(uint8_t *pFrame) = 0; virtual void ExtractData(uint8_t *pFrame) = 0; }; #endif // RAP1_H Well, the implementation of some methods:
FrameSelector::FrameSelector(Ui::MainWindow *UI) { uint32_t keyid = MODEM_DATA * 65536 + SEND_DATA_TO_MODEM; framemap.insert(pair<uint32_t,unique_ptr<RAP1> >(keyid, unique_ptr<DataToModem>(new DataToModem(UI)))); keyid = WAVEFORM_CONFIGURATION * 65536 + PSK_OTA_SET; framemap.insert(pair<uint32_t,unique_ptr<RAP1> >(keyid, unique_ptr<PskOtaSetFrame>(new PskOtaSetFrame(UI)))); } RAP1::RAP1(Ui::MainWindow *UI) : RIPC(UI) {} RAP1::~RAP1() {} PS with shared_ptr doesn't work either.
Addition:
#ifndef RIPC_H #define RIPC_H #include <stdint.h> #include "framecontainer.h" #include "ccitt_crc16.h" // ----------
#ifndef FRAMECONTAINER_H #define FRAMECONTAINER_H #include <stdint.h> #include <memory> #include "ui_mainwindow.h" // ----------
#ifndef USERTYPES_H #define USERTYPES_H #include <stdint.h> / ------ ui_mainwindow.h is generated by the environment itself
#ifndef UI_MAINWINDOW_H #define UI_MAINWINDOW_H #include <QtCore/QVariant> #include <QtWidgets/QAction> #include <QtWidgets/QApplication> #include <QtWidgets/QButtonGroup> #include <QtWidgets/QComboBox> #include <QtWidgets/QFrame> #include <QtWidgets/QGridLayout> #include <QtWidgets/QGroupBox> #include <QtWidgets/QHeaderView> #include <QtWidgets/QLabel> #include <QtWidgets/QLineEdit> #include <QtWidgets/QMainWindow> #include <QtWidgets/QMenuBar> #include <QtWidgets/QProgressBar> #include <QtWidgets/QPushButton> #include <QtWidgets/QRadioButton> #include <QtWidgets/QSpacerItem> #include <QtWidgets/QSpinBox> #include <QtWidgets/QStatusBar> #include <QtWidgets/QTabWidget> #include <QtWidgets/QTextEdit> #include <QtWidgets/QToolBar> #include <QtWidgets/QWidget>