簡體   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