如何使用具有默认值的参数创建函数原型?

时间:2021-01-15 20:14:02

A have a function with a prototype of:

A具有以下原型的功能:

void arryprnt(int[], string, int, string, string);

And a definition of:

并定义:

void arryprnt(int[] a, string intro, int len, string sep=", ", string end=".") {
// stuff
}

And I'm calling it like this:

而我这样称呼它:

arryprnt(jimmy, "PSEUDOJIMMY: ", 15);

...When I make that call to arryprnt, I get a compiler error saying that I've used too few arguments, based off what the prototype says. "Okay," I'm thinking, "The compiler doesn't know that some of arryprnt's parameters have default values. I'll just copy the parameters from the definition into the prototype." And I did, however, I got a compiler error telling me that I was calling arryprnt with too many arguments! I could just explicitly specify all the arguments, but is there any way to call it without specifying all the arguments?

...当我调用arryprnt时,我得到一个编译器错误,说我根据原型所说的使用了太少的参数。 “好的,”我在想,“编译器不知道某些arryprnt的参数有默认值。我只是将参数从定义复制到原型中。”但是,我做了一个编译器错误,告诉我我用太多的参数调用了arryprnt!我可以只显式指定所有参数,但有没有办法调用它而不指定所有参数?

2 个解决方案

#1


You should put the default arguments in the prototype, not the definition like this:

您应该将默认参数放在原型中,而不是像这样的定义:

void arryprnt(int[] a, string intro, int len, string sep=", ", string end=".");

and the make the definition without them:

并且没有它们的定义:

void arryprnt(int[] a, string intro, int len, string sep, string end) {
    // ...
}

BTW: on another note. It is considered good practice to pass objects which larger than an int by const reference. While this isn't appropriate for all situations, it is appropriate for most and avoids copying things unnecessarily. For example:

顺便说一下:另一方面。通过const引用传递大于int的对象被认为是一种好习惯。虽然这不适合所有情况,但它适用于大多数情况并避免不必要地复制内容。例如:

void func(const std::string &s) {
    // do some read-only operation with s.
}

func("hello world");

#2


Not sure if this is still relevant, but below code worked fine.

不确定这是否仍然相关,但下面的代码工作正常。

prototyping:

void arryprnt(int[], string, int, string = ", ", string=". ");

Definition:

void arryprnt(int a[], string intro, int len, string sep, string endd)
//stuff

when there are default arguments for a function, then best is to declare them at prototype, not in function definition. below code didnt work,

当函数有默认参数时,最好是在原型中声明它们,而不是在函数定义中声明它们。下面的代码没有工作,

prototype:

void arryprnt(int[], string, int, string = ", ", string=". ");

definition:

void arryprnt(int a[], string intro, int len, string sep = ", ", string endd = ". ")
//stuff

#1


You should put the default arguments in the prototype, not the definition like this:

您应该将默认参数放在原型中,而不是像这样的定义:

void arryprnt(int[] a, string intro, int len, string sep=", ", string end=".");

and the make the definition without them:

并且没有它们的定义:

void arryprnt(int[] a, string intro, int len, string sep, string end) {
    // ...
}

BTW: on another note. It is considered good practice to pass objects which larger than an int by const reference. While this isn't appropriate for all situations, it is appropriate for most and avoids copying things unnecessarily. For example:

顺便说一下:另一方面。通过const引用传递大于int的对象被认为是一种好习惯。虽然这不适合所有情况,但它适用于大多数情况并避免不必要地复制内容。例如:

void func(const std::string &s) {
    // do some read-only operation with s.
}

func("hello world");

#2


Not sure if this is still relevant, but below code worked fine.

不确定这是否仍然相关,但下面的代码工作正常。

prototyping:

void arryprnt(int[], string, int, string = ", ", string=". ");

Definition:

void arryprnt(int a[], string intro, int len, string sep, string endd)
//stuff

when there are default arguments for a function, then best is to declare them at prototype, not in function definition. below code didnt work,

当函数有默认参数时,最好是在原型中声明它们,而不是在函数定义中声明它们。下面的代码没有工作,

prototype:

void arryprnt(int[], string, int, string = ", ", string=". ");

definition:

void arryprnt(int a[], string intro, int len, string sep = ", ", string endd = ". ")
//stuff