繁体   English   中英

指定默认参数C ++

[英]Specifying default parameters C++

我写了下面的课

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

和功能doJob就像

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

当我编译时,出现以下错误

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

当然这是默认参数规范的问题。那么原型有什么问题?

您不需要重新定义默认值

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

可以只是

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

因为您不需要多次定义参数。

将默认值放在声明中(例如,在示例中的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