【问题标题】:Specifying default parameters C++指定默认参数 C++
【发布时间】:2012-04-14 06:12:12
【问题描述】:

我写了下面的类

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

肯定是默认参数规范的问题..那么原型有什么问题?

【问题讨论】:

    标签: c++ arguments default-arguments


    【解决方案1】:

    你不需要重新定义默认值

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

    只能是

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

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

    【讨论】:

    • 我对上面的“你不需要”和“可以”提出异议。您不能重新定义默认值,第一行必须改为第二行。
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-12-12
        • 1970-01-01
        • 1970-01-01
        • 2020-04-07
        • 1970-01-01
        • 2011-03-24
        • 2010-12-01
        相关资源
        最近更新 更多