Specifying C ++ Default Parameters

I wrote the following class

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

And the doJob function is similar to

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

When I compile, I get the following error

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

This is undoubtedly a problem with the specification of default parameters. So what is the problem with the prototype?

+3
source share
3 answers

You do not need to override the default value

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

maybe just

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

Since you do not need to specify an argument more than once.

+5
source

Put the default value in the declaration (i.e. inside class workerin your example), but not in the definition, for example. simply:

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

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

+1

All Articles