#c 11
Вопрос:
Я получаю эту ошибку компиляции при попытке компиляции inher2.cpp Как правильно назначить запрос событий m_eventQueue ? Я сделал этот MSVP, чтобы посмотреть, смогу ли я продолжить, но не добился успеха.
inher2.cpp:7:26: error: binding reference of type ‘std::shared_ptrlt;EventQueuegt;amp;amp;’ to ‘std::remove_referencelt;const std::shared_ptrlt;EventQueuegt;amp;gt;::type {aka const std::shared_ptrlt;EventQueuegt;}’ discards qualifiers m_eventQueue = std::move(eventQueue); ~~~~~~~~~^~~~~~~~~~~~
inher2.cpp
#includelt;iostreamgt; #include"inher2.hpp" RecordingConfigJobStateSignal::RecordingConfigJobStateSignal( const EventQueuePtramp; eventQueue ) { /* m_eventQueue is actually from class commonQueue */ m_eventQueue = std::move(eventQueue); } int main() { return 0; }
2.ГЭС
#includelt;iostreamgt; #includelt;memorygt; #includelt;queuegt; using namespace std; class EventBase { public: private: int a; }; using EventBasePtr = std::shared_ptrlt;EventBasegt;; class SubscriptionManager { public: int x; }; class EventQueue { public: explicit EventQueue( SubscriptionManageramp; ); ~EventQueue(); EventQueueamp; operator = (const EventQueue amp;) { return *this; } private: std::queuelt; EventBasePtr gt; m_queue; }; using EventQueuePtr = std::shared_ptrlt;EventQueuegt;; class commonQueue { public: int *a; static std::queuelt; EventBasePtr gt; m_queue; const EventQueuePtr m_eventQueue; }; class RecordingConfigJobStateSignal: public commonQueue { public: int c; RecordingConfigJobStateSignal( const EventQueuePtr amp;); private: int b; };
Я исправил одну ошибку до этого, используя std::move, но застрял на ошибке, описанной выше.
inher2.cpp:7:17: error: cannot bind rvalue reference of type ‘std::shared_ptrlt;EventQueuegt;amp;amp;’ to lvalue of type ‘const EventQueuePtr {aka const std::shared_ptrlt;EventQueuegt;}’ m_eventQueue = eventQueue; ^~~~~~~~~~
Комментарии:
1. Остается только один вопрос. что, если бы я хотел, чтобы m_eventQueue был постоянным членом в классе commonQueue. что мне тогда следовало делать ? Кажется, я снова и снова спотыкаюсь о константы.
Ответ №1:
class commonQueue { public: int *a; static std::queuelt; EventBasePtr gt; m_queue; const EventQueuePtr m_eventQueue; };
Я понял, что это не обязательно должен быть постоянный член.
Не требуется оператор назначения в
EventQueueamp; operator = (const EventQueue amp;) { return *this; }
Ненужным и усложнил его еще больше движением, которое мне не понадобилось, как только я исправил два вышеперечисленных.
m_eventQueue = std::move(eventQueue);