Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifying default parameters C++

I have written the following class

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

And the function doJob is like

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

When i compile ,i am getting the following error

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

Surely it is a problem with default parameter specification..So what is the problem with th e prototype?

like image 416
Jinu Joseph Daniel Avatar asked Sep 18 '25 20:09

Jinu Joseph Daniel


2 Answers

You don't need to redefine the default value

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

can just be

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

As you do not need to define the argument more than once.

like image 94
josephthomas Avatar answered Sep 20 '25 09:09

josephthomas


Put the default in the declaration (ie inside class worker in your example), but not in the definition, e.g. code simply:

 int worker::doJob(int type,int time)
 {  /* your code here */ }
like image 38
Basile Starynkevitch Avatar answered Sep 20 '25 08:09

Basile Starynkevitch