Mixing default and non-default parameters in Constructor?

D

desktop

Are there some way to mix default and non-default parameters in a
constructor:

class test {

public:
test(int i = 45, int j) : pp(j){}
private:
int pp;
int i;
};

The above does not work and gives the error:

main.cpp:144: error: default argument missing for parameter 2 of
‘test::test(int, int)’
make: *** [main.o] Error 1
 
R

red floyd

desktop said:
Are there some way to mix default and non-default parameters in a
constructor:

class test {

public:
test(int i = 45, int j) : pp(j){}
test(int j, int i_ = 45) : pp(j), i(i_) { }
private:
int pp;
int i;
};

The above does not work and gives the error:

main.cpp:144: error: default argument missing for parameter 2 of
‘test::test(int, int)’
make: *** [main.o] Error 1


Default parameters must be at the *end* of the parameter list.
Note also, that you did not initialize your member variable "i".
 
V

Victor Bazarov

desktop said:
Are there some way to mix default and non-default parameters in a
constructor:

class test {

public:
test(int i = 45, int j) : pp(j){}
private:
int pp;
int i;
};

The above does not work and gives the error:

main.cpp:144: error: default argument missing for parameter 2 of
‘test::test(int, int)’
make: *** [main.o] Error 1

Yes, but default arguments have to be the last one.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,830
Latest member
ZADIva7383

Latest Threads

Top