Elements of a structure on init list

Hi, I've got a problem with initialize list which include elements of the structure.

Content of the calendar.h file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct Date {
	int day;
	int month;
	int year;
};

class calendar{
	private:
		struct Date date;
		int week_day;
	public:
		calendar();
		calendar(const calendar&);
		calendar( int, int, int);
};


Content of the calendar.cpp:
1
2
#include "calendar.h"
calendar::calendar(int d, int m, int y) : date.day(d), date.month(m), date.year(y) {}


These are errors i got:

expected '(' before '.' token calendar.cpp /rogus1 line 5 C/C++ Problem
expected '{' before '.' token calendar.cpp /rogus1 line 5 C/C++ Problem
expected unqualified-id before '.' token calendar.cpp /rogus1 line 5 C/C++ Problem

Thanks for any help!
Last edited on
If you have a compiler that does not support the C++ Standard 2011 then define a constructor for struct Data ( or Date?!)

struct Data {
Data( int day, int month, int year ) : day( day ), month( month ), year( year ) {}

int day;
int month;
int year;
};

And in calendar use the following construction


calendar::calendar(int d, int m, int y) : data( d, m, y ) {}
Yeah, exactly Date, i've corrected my post ( i translate the code from the other language, hence that mistatake).

But now in compilation i receive the following error:

no matching function for call to 'Date::Date()' kalendarz.cpp /rogus1 line 5 C/C++ Problem
Last edited on
I think that you forgot to define a default constructor for Date which is called from the constructor calendar();

So define a default constructor for date. For example

Date::Date() : day( 0 ), month( 0 ), year( 0 ) {}
Nice, it works now. Thanks a lot!
Topic archived. No new replies allowed.