简体   繁体   English

指定默认参数C ++

[英]Specifying default parameters C++

I have written the following class 我写了下面的课

class worker
{
   int action;
   int doJob(int type,int time = 0);
   public:
   int call();
}

And the function doJob is like 和功能doJob就像

int worker::doJob(int type,int time = 0)
{
          ....code here
}

When i compile ,i am getting the following error 当我编译时,出现以下错误

 error: the default argument for parameter 1 of 'int worker::doJob(int, int)' has not yet been parsed

Surely it is a problem with default parameter specification..So what is the problem with th e prototype? 当然这是默认参数规范的问题。那么原型有什么问题?

You don't need to redefine the default value 您不需要重新定义默认值

int worker::doJob(int type,int time = 0)

can just be 可以只是

int worker::doJob(int type,int time)

As you do not need to define the argument more than once. 因为您不需要多次定义参数。

Put the default in the declaration (ie inside class worker in your example), but not in the definition, eg code simply: 将默认值放在声明中(例如,在示例中的class worker中),而不是在定义中,例如,简单地编写代码:

 int worker::doJob(int type,int time)
 {  /* your code here */ }

int worker::doJob(int type,int time = 0)给您一个错误,您只应声明一次默认参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM