0

I have a problem with a default constructor in C++. It's a simple thing but can't see what's wrong with it.

I have a constructor with 3 optional parameters, with const values on initialization list:

data::data(int D = 1, int M = 1, int Y = 1583) : Day(D), Month(M), Year(Y)  
{  
    if (!CorrectDate()) throw "Wrong Date!";  
}  

Why can I call it with one, two or three parameters and it works just fine but doesn't when I call it with no parameters?

data tommorrow();
Donato Szilagyi
  • 4,279
  • 4
  • 36
  • 53
mechu
  • 278
  • 2
  • 9
  • See: [Most vexing parse: why doesn't A a(()); work?](http://stackoverflow.com/questions/1424510/most-vexing-parse-why-doesnt-a-a-work). The short answer is, you are declaring a function there. If you want to call the default constructor, leave out the parens. – Jeff Mercado Mar 22 '11 at 01:17

4 Answers4

3

data tomorrow(); is a declaration of a function that returns a data and takes no parameters. To create a data object with no explicit constructor arguments, just do data tomorrow; without the parentheses.

Michael Ratanapintha
  • 39,422
  • 4
  • 33
  • 40
2

Define it as

data tomorrow;

data tomorrow(); is the same as defining a function called tomorrow which returns data

Adam Casey
  • 1,600
  • 12
  • 21
1

You are probably doing something like

data something();

which is not an initialization of a variable of type data called something, but the declaration of a function called something that returns data.

If this is the case, the correct would be:

data something;
Jon
  • 428,835
  • 81
  • 738
  • 806
0

You are declaring a function which returns a data, you can do either:

data tommorow;

Without (), or you can do:

data tommorow = data();
fbafelipe
  • 4,862
  • 2
  • 25
  • 40