gpt4 book ai didi

c++ - 为什么使用 std::bind 出现参数错误

转载 作者:行者123 更新时间:2023-11-28 05:41:46 25 4
gpt4 key购买 nike

<分区>

我正在学习 c++11 规范。

使用std::bind,我不明白为什么会发生错误。

下面是简单的代码。

class ClassInfo
{
public:
ClassInfo() : nID(0) {}
~ClassInfo() {}

bool GetInfo1(int nVal1, int& nOutVal1, std::string& strOut1, std::string& strOut2 )
{
nOutVal1 = nID;
strOut1 = "out1";
strOut2 = "out2";
return true;
}
bool GetInfo2(int nVal1, int& nOutVal1, std::string& strOut1, std::string& strOut2, std::string& strOut3 )
{
nOutVal1 = nID;
strOut1 = "out1";
strOut2 = "out2";
strOut3 = "out3";
return true;
}

int nID;
};

int main()
{
std::shared_ptr<ClassInfo> spInfo = std::make_shared<ClassInfo>();
spInfo->nID = 10;

int nVal1 = 5;
int nOutVal1;
std::string strOut1;
std::string strOut2;
std::string strOut3;

auto _func1 = std::bind(&ClassInfo::GetInfo1, spInfo, nVal1, std::ref(nOutVal1), std::ref(strOut1), std::ref(strOut2));
_func1();

auto _func2 = std::bind(&ClassInfo::GetInfo2, spInfo, nVal1, std::ref(nOutVal1), std::ref(strOut1), std::ref(strOut2), std::ref(strOut3));
_func2();
}

此代码未编译..

auto _func1 = std::bind(&ClassInfo::GetInfo1, spInfo, nVal1, std::ref(nOutVal1), std::ref(strOut1), std::ref(strOut2));

这没关系,但是

auto _func2 = std::bind(&ClassInfo::GetInfo2, spInfo, nVal1, std::ref(nOutVal1), std::ref(strOut1), std::ref(strOut2), std::ref(strOut3));

此代码出现编译错误。为什么会出错?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com