在 C++ 中定义默认函数参数构建器类和参数太少错误
我有一个类定义了一个带有默认参数的函数。只要函数定义位于调用它的函数之前的头文件中,它就可以正常工作。
但是,如果我在调用函数 C++ Builder (2010) 报告参数太少错误后移动它。
例如,标头可能是:
class TSomething
{
public:
void CallingFunction();
void Function(int a);
}
并且 cpp 文件可能是:
#include "Header.h"
TSomething::CallingFunction()
{
Function(); // "Too few arguments" here...
}
TSomething::Function(int a = 123)
{
//... some code here ...
}
因此,如果调用函数在“函数”之前,则会报告参数太少。我不明白为什么,因为它在 cpp 文件中的 #include 语句中包含函数定义。谁能告诉我如何重新排列它以便它接受正确的默认参数?我可以将 Function(int a) 移至 CallingFunction 上方以使其正常工作。
I have a class that defines a function with default parameters. It works fine as long as function definition is in header file before function that calls it.
However if I move it after the calling function C++ Builder (2010) reports Too few parameters error.
header might be for example:
class TSomething
{
public:
void CallingFunction();
void Function(int a);
}
and cpp file might be:
#include "Header.h"
TSomething::CallingFunction()
{
Function(); // "Too few arguments" here...
}
TSomething::Function(int a = 123)
{
//... some code here ...
}
So if calling function is before "Function" it reports Too few parameters. I do not understand why because it includes function definition in #include statement in cpp file. Can anyone tell me how to rearrange this so it accepts properly default arguments? I can move the Function(int a) above the CallingFunction to make it work so far.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将默认参数放在类内部的成员函数声明中:
此外,从类外部的定义中删除默认参数。
You need to put the default arguments in member function's declaration inside your class:
Also, remove the default arguments from the definition outside of your class.