Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Error: reference to type 'NBhistoryItem *const' could not bind to an lvalue of type 'const NBhistoryItem *'



  • Hello,

    I am getting this error:
    error: reference to type 'NBhistoryItem *const' could not bind to an lvalue of type 'const NBhistoryItem *'

    whenever the following code is called:
    void NBhistoryInterface::addHistoryItem(const NBhistoryItem *input)
    @
    {
    qDebug() << "SLOT: NBhistoryInterface::addHistoryItem(const NBhistoryItem &input) STATUS: Called";
    m_history.prepend(input);
    qDebug() << "SLOT: NBhistoryInterface::addHistoryItem(const NBhistoryItem &input) STATUS: Completed";
    }
    @

    m_history is a QList<NBhistoryItem*>

    What am I doing wrong?

    Thanks!



  • Your list is of pointers to non-const NBhistoryItems and you are trying to prepend a pointer to a const NBhistoryItem.



  • okay, so then what should it look like?



  • Either this (if the NBhistoryItems should be modifiable through the pointers in the list):
    @
    QList<NBhistoryItem*> m_list;

    void NBhistoryInterface::addHistoryItem(NBhistoryItem input)
    {
    m_history.prepend(input);
    }
    @
    or this (if the list should be pointing to const NBhistoryItems):
    @
    QList<const NBhistoryItem
    > m_list;

    void NBhistoryInterface::addHistoryItem(const NBhistoryItem *input)
    {
    m_history.prepend(input);
    }
    @



  • Ok changed it to the first option you gave:

    @
    void NBhistoryInterface::addHistoryItem(NBhistoryItem *input)
    {
    qDebug() << "SLOT: NBhistoryInterface::addHistoryItem(const NBhistoryItem &input) STATUS: Called";
    m_history.prepend(input);
    qDebug() << "SLOT: NBhistoryInterface::addHistoryItem(const NBhistoryItem &input) STATUS: Completed";
    }
    @

    The program compiles then crashes. Heres what the crash report gives me:

    Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
    0 @BUNDLEIDENTIFIER@ 0x0000000100018364 QList<NBhistoryItem*>::prepend(NBhistoryItem* const&) + 20 (qlist.h:546)
    1 @BUNDLEIDENTIFIER@ 0x000000010001795e NBhistoryInterface::addHistoryItem(NBhistoryItem*) + 142 (nbhistory.cpp:



  • Did you recompile the everything in the code that uses that NBhistoryInterface::addHistoryItem() interface?



  • yes, I believe everything has been recompiled. Maybe the problem has to do with the slot calling addHistoryItem(), but I don't think so. Here it is anyway though:
    @
    void NBhistoryInterface::addHistoryEntry(const QUrl &url)
    {
    QUrl historyUrl = url;
    NBhistoryItem *item = new NBhistoryItem("url", "title");
    addHistoryItem(item);
    }

    void NBhistoryInterface::addHistoryItem(NBhistoryItem *input)
    {
    qDebug() << "SLOT: NBhistoryInterface::addHistoryItem(const NBhistoryItem &input) STATUS: Called";
    m_history.prepend(input);
    qDebug() << "SLOT: NBhistoryInterface::addHistoryItem(const NBhistoryItem &input) STATUS: Completed";
    }

    QList<NBhistoryItem*> m_history;
    @

    Thanks for the help!


Log in to reply