I need clarification on how class functions work

I'm getting this error when I compile this code.

/*Error error C2440: 'initializing' : cannot convert from 'void' to 'Chrono::Date'*/

I believe this comes from me misunderstanding how class functions and objects work.
The compiler reports the error from Chrono::Date tomorrow = today.add_day(1);

*I know "Date" is better represented as a class rather than a struct, however I'm currently working through an exercise in a book*

Chrono.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include "std_lib_facilities.h";

namespace Chrono
{
	/*enum class Month
	{
		jan = 1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec
	};*/
	struct Date
	{
		class Invalid{};

		Date();
		Date(int m, int d, int y);

		void add_day(int n);

		int year;
		int month;
		int day;
	};

	bool is_date(int m, int d, int y);
	bool leapyear(int y);

	ostream& operator<<(ostream& os, const Date& d);



}


Chrono.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "std_lib_facilities.h";
#include "Chrono.h";

namespace Chrono
{
	Date::Date()
		:month{ 1 }, day{ 1 }, year{1970}
	{

	}
	Date::Date(int m, int d, int y)
		: month{ m }, day{ d }, year{y}
	{
		if (!is_date(m, d, y)) throw Invalid{};
	}
	void Date::add_day(int n)
	{
		int days_in_month = 31;

		switch (month)
		{
		case 2:
			days_in_month = (leapyear(year)) ? 29 : 28;
			break;
		case 4: case 6: case 9: case 11:
			days_in_month = 30;
			break;
		}
		for (int i = 0; i < n; i++)
		{
			day = (day == days_in_month) ? 1 : day + 1;
		}
	}

	ostream& operator<<(ostream& os, const Date& d)
	{
		return os << "(" << d.year << ", " << d.month << ", " << d.day << ")";
	}
	bool is_date(int m, int d, int y)
	{
		if (d < 0) return false;
		if (y < 0) return false;
		if (m < 1 || 12 < m) return false;

		int days_in_month = 31;

		switch (m)
		{
		case 2:
			days_in_month = (leapyear(y)) ? 29 : 28;
			break;
		case 4: case 6: case 9: case 11:
			days_in_month = 30;
			break;
		}

		if (days_in_month < d) return false;
		
		return true;
	}

	bool leapyear(int y)
	{
		if (y % 4 != 0) return false;
		if (y % 100 == 0)
		{
			if (y % 400 != 0) return false;
		}
		return true;
	}
}





Source.cpp
1
2
3
4
5
6
7
8
9
10
#include "std_lib_facilities.h";
#include "Chrono.h";

int main()
{
	Chrono::Date today = Chrono::Date(6, 25, 1978);
	Chrono::Date tomorrow = today.add_day(1);

	keep_window_open();
}
Last edited on
Remove all the semi-colons after the #includes. They don't belong there.
Thanks I didn't know that.
Last edited on
Still need help with issue though
void add_day(int n);

This function has void as the return type. Therefore the function does not return anything.

Chrono::Date tomorrow = today.add_day(1);

This line attempts to assign whatever today.add_day returns to your 'tomorrow' object. However add_day doesn't return anything, so you get this error.

What's more... add_day actually modifies the object. You are actually adding 1 day to 'today'.

So instead, you could do this:

1
2
3
4
5
6
int main()
{
    Chrono::Date today( 6, 25, 1978 ); // <- easier way to construct the object
    Chrono::Date tomorrow( today );    // <- tomorrow = today
    tomorrow.add_day( 1 );             // <- tomorrow = today + 1
}
Last edited on
So if I changed add_day() to return a Date, it would compile?
Topic archived. No new replies allowed.