0

I have created a parameterized constructor with default arguments as follows:

Rectangle(int l=2,int b=2)
{
    setLength(l);
    setBreadth(b);
}

When I'm creating object like

Rectangle r; 

It is taking both default arguments.

When

Rectangle r(10);

It takes another default argument.

But when

Rectangle r();

It's not working and throws and error,rather than taking both the default arguments.

Udit Yadav
  • 99
  • 1
  • 7
  • 1
    A _**quick**_ search on the web would have given you plenty of answers. You're being victime of the [Most Vexing Parse (MVP)](https://en.wikipedia.org/wiki/Most_vexing_parse). – Enlico Apr 04 '21 at 06:22
  • 2
    @Enlico That only works if you already know about the "Most Vexing Parse"; and then you didn't need to do the search! – Mike Housky Apr 04 '21 at 06:24
  • In my case, it is compiling and (probably) invoking default ctor (and no, I haven't explicitly provided one). (MSVC C++17) – kesarling He-Him Apr 04 '21 at 06:29
  • @MikeHousky, the OP sees an error occurring probably on a line like `r.setLength(bla)` or something (not on the line `Rectangle r();`, because that is mis-interpreted, but ok), and that error is probably along the lines of `error: request for member ‘getLength’ in ‘r’, which is of non-class type ‘Rectangle()’`. If the OP searched for `"which is of non-class type" c++`, he would have got [relevant results](https://stackoverflow.com/search?q=%5Bc%2B%2B%5D%22which+is+of+non-class+type%22). – Enlico Apr 05 '21 at 06:09

1 Answers1

-2

Instead of using Rectangle r(); you should be using just Rectangle r; for using default arguments. Using Rectangle r(); gives error as use are kind on calling an object like a function.

nimrobotics
  • 114
  • 1
  • 8
  • 2
    *"as use are kind on calling an object like a function"* no idea what you're trying to tell us with this part of the sentence. I don't see this being close enough to what's actually causing the error to refrain from downvoting here. – fabian Apr 04 '21 at 06:40