指定默认参数C++

Jin*_*iel 3 c++ arguments default-arguments

我写了下面的课

class worker
{
   int action;
   int doJob(int type,int time = 0);
   public:
   int call();
}
Run Code Online (Sandbox Code Playgroud)

功能doJob就像

int worker::doJob(int type,int time = 0)
{
          ....code here
}
Run Code Online (Sandbox Code Playgroud)

当我编译时,我收到以下错误

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

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

jos*_*mas 5

您无需重新定义默认值

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

可以

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

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

  • 我对上面的"你不需要"和"可以只是"提出异议.你__ not't_重新定义默认值,第一行_must_被改为第二行. (3认同)