3

我写了以下课程

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

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

4

3 回答 3

5

您不需要重新定义默认值

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

只能是

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

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

于 2012-04-14T06:13:28.320 回答
4

将默认值放在声明中(即class worker在您的示例中),但不在定义中,例如代码:

 int worker::doJob(int type,int time)
 {  /* your code here */ }
于 2012-04-14T06:13:50.993 回答
1

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

于 2012-04-14T06:15:12.953 回答