QT之error: cannot bind non-const lvalue reference of type ‘CBaowen’ to an rvalue of type ‘CBaowen
生活随笔
收集整理的這篇文章主要介紹了
QT之error: cannot bind non-const lvalue reference of type ‘CBaowen’ to an rvalue of type ‘CBaowen
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
問題描述:QT中自己編寫了一個結構體變量CBaowen,報文中含有函數重載部分(如下所示),
之后定義一個隊列變量 QQueue<CBaowen>? queue;
當給隊列變量賦值時queue.enqueue(baowen)時,
會出現cannot bind non-const lvalue reference of type ‘CBaowen&’ to an rvalue of type ‘CBaowen錯誤
CBaoWen& operator=(CBaoWen &OtherBaoWen) //賦值運算符重載{}CBaoWen(CBaoWen &OtherBaoWen) //拷貝構造函數{} 問題原因:這個錯誤是C++編譯器的一個關于語義的限制。(重載函數中)
如果一個參數是以非const引用傳入,c++編譯器就有理由認為程序員會在函數中修改這個值,并且這個被修改的引用在函數返回后要發揮作用。但如果你把一個臨時變量當作非const引用參數傳進來,由于臨時變量的特殊性,程序員并不能操作臨時變量,而且臨時變量隨時可能被釋放掉,所以,一般說來,修改一個臨時變量是毫無意義的,據此,c++編譯器加入了臨時變量不能作為非const引用的這個語義限制。
解決辦法:
只需給=重載參數加上const常量限制符。
CBaoWen& operator=(const CBaoWen &OtherBaoWen) //賦值運算符重載{}CBaoWen(const CBaoWen &OtherBaoWen) //拷貝構造函數{} 參考:https://www.cnblogs.com/area-h-p/p/11498481.html總結
以上是生活随笔為你收集整理的QT之error: cannot bind non-const lvalue reference of type ‘CBaowen’ to an rvalue of type ‘CBaowen的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c++primer12.3文本查询程序的
- 下一篇: s3c2440移植MQTT