久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

錯誤:無法將“int&"類型的非常量左值引用

Error: cannot bind non-const lvalue reference of type ‘intamp;’ to an rvalue of type ‘int’(錯誤:無法將“int類型的非常量左值引用綁定到“int類型的右值)
本文介紹了錯誤:無法將“int&"類型的非常量左值引用綁定到“int"類型的右值的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我需要創(chuàng)建一個 Bar 對象,它有一個私有對象 Foo f.

I need to create a Bar object, which has a private object Foo f.

但是Foo對象參數(shù)的值應(yīng)該通過特定的方法int genValue()來傳遞.

However, the value of Foo object parameter should be passed by the specific method int genValue().

如果我在構(gòu)造函數(shù)作用域 Bar(){...} 中初始化 f,編譯器會大喊錯誤,就像沒有構(gòu)造函數(shù) Foo().

If I initialize f in the constructor scope Bar(){...}, the compiler yell error, something like there is no constructor Foo().

如果我像這樣構(gòu)造Bar(): f(genValue()),編譯器會報錯:

If I construct like this Bar(): f(genValue()), the compiler yells the error:

test.cpp: In constructor ‘Bar::Bar()’:
test.cpp:16:19: error: cannot bind non-const lvalue reference of type ‘int&’ to an rvalue of type ‘int’
 Bar(): f(genValue()){    
            ~~~~~~~~^~
test.cpp:7:2: note:   initializing argument 1 of ‘Foo::Foo(int&)’    
 Foo(int &x) {    
 ^~~

示例代碼:

class Foo {
public:
    Foo(int &x) {
        this->x = x;
    }
private:
    int x;
};

class Bar {
public:
    Bar(): f(genValue()){
    }
private:
    Foo f;

    int genValue(){
        int x;
        // do something ...
        x = 1;
        return x;
    }
};

int main() {

    Bar bar ();

    return 0;
}

如果我不想修改 Foo 類并且它的參數(shù)值應(yīng)該從 genValue() 傳遞,我該如何解決這個問題?而且,我不想使用純指針 (*),但使用智能指針的解決方案是可以的!

How can I fix the problem, if I don't want to modify Foo class and its argument value should be passed from genValue()? And, I don't want to use pure pointer (*), but a solution with smart pointer is okay!

推薦答案

const 引用參數(shù),例如int&,只能引用一個左值",這是一個命名變量.

A non-const reference parameter, such as an int&, can only refer to an "lvalue," which is a named variable.

auto takes_nonconst_reference = [](int&){};
auto takes_const_reference = [](const int&){};
auto takes_value = [](int){};
auto returns_int = []{return 42;};

int foo = 1;

// OK
takes_nonconst_reference(foo);
takes_const_reference(foo);
takes_const_reference(returns_int());
takes_value(foo);
takes_value(returns_int());

// compilation error, value returned from a function is not a named variable
takes_nonconst_reference(returns_int());

在這種特殊情況下,由于您的類存儲了構(gòu)造函數(shù)參數(shù)的副本,您應(yīng)該按值傳遞它(int,而不是 int&const int&).

In this particular case, since your class is storing a copy of the constructor parameter, you should pass it by value (int, not int& nor const int&).

這篇關(guān)于錯誤:無法將“int&"類型的非常量左值引用綁定到“int"類型的右值的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Assertion failed (size.widthgt;0 amp;amp; size.heightgt;0)(斷言失敗(size.width0 amp;amp; size.height0))
Rotate an image in C++ without using OpenCV functions(在 C++ 中旋轉(zhuǎn)圖像而不使用 OpenCV 函數(shù))
OpenCV: process every frame(OpenCV:處理每一幀)
Why can#39;t I open avi video in openCV?(為什么我不能在 openCV 中打開 avi 視頻?)
OpenCV unable to set up SVM Parameters(OpenCV 無法設(shè)置 SVM 參數(shù))
Convert a single color with cvtColor(使用 cvtColor 轉(zhuǎn)換單一顏色)
主站蜘蛛池模板: 99精品视频免费观看 | 日本不卡一区二区三区 | 久久一区二区三区免费 | 国产精品久久久久久婷婷天堂 | 国产视频一区在线 | 日韩免费中文字幕 | 国产精品久久久久国产a级 欧美日本韩国一区二区 | 国产精品美女久久久久aⅴ国产馆 | 天天操操| 中文字幕 国产精品 | 精品国产91亚洲一区二区三区www | 日韩国产一区二区三区 | 日日久| 亚洲欧美一区二区三区在线 | 国产精品日本一区二区不卡视频 | 天天艹 | 亚洲一区 中文字幕 | 亚洲欧美在线视频 | 国产一级片网站 | 红桃视频一区二区三区免费 | www日本在线播放 | 就操在线| 天堂久久天堂综合色 | 欧美成人a∨高清免费观看 老司机午夜性大片 | 久久天堂网 | 午夜爱爱网 | 黄色毛片黄色毛片 | 欧美中文字幕在线观看 | 中文字幕av一区 | 国产精品国产精品国产专区不片 | 欧美日韩高清在线一区 | 成人国产精品久久 | 欧美精品啪啪 | 台湾a级理论片在线观看 | 天天看天天操 | 欧洲一级毛片 | 亚洲精品在线观 | 国产精品久久9 | 亚洲精彩视频在线观看 | 99精品视频在线观看免费播放 | 91一区二区三区在线观看 |