0

I have a Regular class. It has a setValue method:

void Regular::setValue(string id, string name, double s, int n = 0)
{
    uid = id;
    uname = name;
    sid = s;
    netcount = n;

}

When I run the following code

Regular x;
x.setValue("X01", "John Doe", 1.1);

It gives me an error

'Regular::setValue': function does not take 3 arguments 

Should it not set the value of netcount = 0 by default, because I didn't pass in the fourth argument?

alexis
  • 31
  • 6
  • 3
    you need to have the default value at the function declaration visible to the calling code. not necessarily at the definition of the function – Tyker Jun 29 '18 at 12:19
  • Have you declared last argument as default argument in function declaration also.? setValue(string id, string name, double s, int n = 0) – yadhu Jun 29 '18 at 12:20
  • Please show a [mcve]. It's quite likely the calling code doesn't see the default argument. – Angew is no longer proud of SO Jun 29 '18 at 12:20
  • Turns out I must not declare the last argument as default argument in the function itself, only the function declaration. Thanks! Is it possible to set it in the constructor instead? – alexis Jun 29 '18 at 12:26

1 Answers1

0

This snippet should solve your problem. Only in declaration u need to specify as default argument.

#include <iostream>
#include <string>

using namespace std;

class Regular {
    string uid;
    string uname;
    double sid;
    int netcount;

public:
    void setValue(string id, string name, double s, int n = 0);
};


void Regular::setValue(string id, string name, double s, int n)
{
    uid = id;
    uname = name;
    sid = s;
    netcount = n;

}

int main()
{
    Regular x;
    x.setValue("X01", "John Doe", 1.1);
    return 0;
}
yadhu
  • 1,253
  • 14
  • 25