C++添加默认参数的特定语法

发布于 2024-12-20 23:29:05 字数 667 浏览 2 评论 0原文

想象一下,您的代码库中有一个函数 X()。它需要三个参数。原型看起来像这样:

在头文件中:

void X(string a, int b, string c);

在 .cpp 文件中:

void myclass::X(string a, int b, string c) {
...
}

假设您要添加到代码库,并且特别需要传递第四个参数 只在一个地方到达X。 3 个参数的调用都需要保留在原处。您希望避免破坏对 X 的调用的所有 3 参数版本。

我已尝试使用此处的建议但这行不通。答案一仍然会产生找不到匹配函数的错误。尝试执行答案 2 建议的操作会产生错误,导致我无法重载该函数。具体来说,我根据该链接中的答案 2 尝试此操作:

在头文件中:

void X(string a, int b, string c, string d);    
void X(string a, int b, string c, string d="default_value");

非常感谢有关如何正确添加单个 4 参数调用而不破坏所有 3 参数调用的任何建议 - 谢谢!

Imagine you have a function X() all over your codebase. It takes three arguments. The prototype looks something like this:

In header file:

void X(string a, int b, string c);

In .cpp file:

void myclass::X(string a, int b, string c) {
...
}

Suppose you are adding to the codebase, and have a particular need to pass a 4th argument
to X in only one spot. The 3-argument calls all need to remain in place. You would like to avoid breaking all of the 3-argument versions of the calls to X.

I have tried using the advice here but this doesn't work. Answer one still produces errors of no matching function found. Trying to do what answer 2 suggestions yields errors that I can't overload the function. Specifically, I try this, based on answer 2 in that link:

In header file:

void X(string a, int b, string c, string d);    
void X(string a, int b, string c, string d="default_value");

Any suggestions on how to correctly add a single 4-argument call without breaking all the 3-argument calls is much appreciated - thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

[旋木] 2024-12-27 23:29:05

只需添加一个新原型,保持前一个原型不变:

string X(string a, int b, string c);    
string X(string a, int b, string c, string d);

您不能为新参数分配默认值,因为这会导致歧义:编译器不可能知道您要调用哪个方法。

或者,您可以用新定义替换旧定义,采用一个额外参数,并为其设置默认值。但在这种情况下,您必须删除以前的函数声明。

提示:

根据您的语法,我认为您正在

using namespace std;

标头中使用。这是不好的做法。这样做会在包含此标头的任何位置使用 std 命名空间填充全局命名空间。您应该删除此指令并改为限定您的类型:std::string 而不是 string

Just add a new prototype, leaving the previous one unchanged:

string X(string a, int b, string c);    
string X(string a, int b, string c, string d);

You can't assign a default value to the new argument, since that would cause an ambiguity: the compiler can't possibly know which of the methods you want to call.

Or you could replace the old definition with a new one, taking one extra parameter, and set a default for that. But in this case, you have to remove the previous function declaration.

HINT:

Per your syntax, I think you're using

using namespace std;

in your header. This is bad practice. Doing so populates the global namespace with the std namespace wherever you include this header. You should remove this directive and instead qualify your types: std::string instead of string

抠脚大汉 2024-12-27 23:29:05

在头文件中,您需要有两个函数

void string X(string a, int b, string c);
void string X(string a, int b, string c, string d);

这样,使用三个参数调用 X 的所有代码段都不会中断,并且使用四个参数调用 X 的单个位置将被调用(即,调用适当的重载函数)。

In header file you need to have two functions

void string X(string a, int b, string c);
void string X(string a, int b, string c, string d);

This way all the code segment that calls X with three arguments don't break and the single place where you are calling X with four arguments gets called (i.e., appropriate overloaded function gets called).

去了角落 2024-12-27 23:29:05

您只需要头文件中的第二个定义。两者同时存在就是造成过载错误的原因。

void string X(string a, int b, string c, string d="default_value");

意味着,当给出 4 个值时,使用提供的 d,当仅给出 3 个值时,将 d 设置为“default_value”。

You only need the second definition in the header file. Having both of them is what's creating the overload error.

void string X(string a, int b, string c, string d="default_value");

Means, when 4 values are given, use the provided d, and when only 3 are given, set d to "default_value".

樱娆 2024-12-27 23:29:05

此示例以最高警告级别进行编译:

#include <iostream>
#include <string>

void foo(std::string , int , std::string , std::string d="default_value")
{
    std::cout<<"d="<<d<<std::endl;
}


int main() {
    foo("param1",55,"param2");
}

foo 函数的最后一个参数具有默认值。

This example compiles with maximum warning level:

#include <iostream>
#include <string>

void foo(std::string , int , std::string , std::string d="default_value")
{
    std::cout<<"d="<<d<<std::endl;
}


int main() {
    foo("param1",55,"param2");
}

The last parameter to the foo function has a default value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文