gpt4 book ai didi

c++ - 如何解决由于参数中的默认构造函数导致的函数重载不明确

转载 作者:行者123 更新时间:2023-11-30 02:12:43 28 4
gpt4 key购买 nike

假设我有这个类(class):

class Foo {
public:
void member(std::string s);
void member(int64_t &n);
};

现在我想做一些事情

int64_t value = 5;
Foo f;
f.member(value);

问题是编译器(至少是 GCC)感到困惑并认为我正在尝试使用 char* 构造函数调用带有字符串的成员:

invalid conversion from 'int64_t' to 'const char*

How could I go about calling the actual member function I want without changing the method signature? Are templates the only solution? I have tried casting without any help (which shouldn't matter since the type is already unambiguous).


Sorry - found the mistake.

The declaration was:

class Foo {
public:
void member(std::string s);
void member(int64_t &n);
};

删除 by-ref 解决了它。

最佳答案

转换参数以完全匹配您想要的重载的参数类型:

f.member((int64_t) value);

关于c++ - 如何解决由于参数中的默认构造函数导致的函数重载不明确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1489125/

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