gpt4 book ai didi

c++ - 尝试使用可变参数编译代码时出错

转载 作者:行者123 更新时间:2023-11-30 02:32:03 26 4
gpt4 key购买 nike

当我尝试编译以下代码时,收到 C2672 和 C2783 错误。我不知道如何修复它。

class Statement
{
public:
template<typename T, typename ... Args>
void Bind_All(Args... args)
{
std::vector<T> list = { args... };
}
}

void func()
{
Statement stmt;
stmt.Bind_All(1, 2.5, "3");
}

error C2672: 'Statement::Bind_All': no matching overloaded function found

error C2783: 'void Statement::Bind_All(Args...)': could not deduce template argument for 'T'

谢谢!

最佳答案

由于 T 不是参数的一部分,编译器无法推断出 T。您需要明确说明。

Statement stmt;
stmt.Bind_All<int>(1, 2.5, "3");

但是,请注意,这将不起作用,因为行中的 '"3"cannot be converted to anint`:

std::vector<T> list = { args... };

调用 stmt.Bind_All() 时,您需要使用合适的参数列表。

关于c++ - 尝试使用可变参数编译代码时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36850092/

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