Code error

hey everyone i need some help on this code. Getting errors in the assignment6.cpp part of the code. i will post the directions i have to go by below and then the code i have.

Date Class:
Design and Implement a class called Date that has data members to store month (as a number), day,
year, and name of the month. The class should have a three-parameter constructor that allows the data
to be set at the time of new Data object instances are created. Default constructor that does not take
any parameters should set the default values of 1 (month), 1 (day), 2001 (year). The class should have
following three member functions to display date following formats
showDate1() should display the date in 1/1/2001 format
showDate2() should display the date in January 1, 2001 format
showDate3() should display the date in 1 January 2001 format
Also the class should have method to set the date (setDate()). This method should take month, day, and
year as parameters and set the object instance data member values. Then your Date class should work
with the Assignment5.cpp given below and produces the following output.
1/1/2001
February 12, 2010
29 August 1986

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
76
77
78
79
80
81
82
83
84
85
 //Date.cpp
#include<iostream>
#include "Date.h"

const string Date::months[12] = { "January", "Febraury", "March", "April", "May","June","July","August","September","October","November","December" };
Date::Date(int m, int d, int y) {
	month = m;
	day = d;
	year = y;
	month_name = months[month - 1];
}

Date::Date() {
	month = 1;
	day = 1;
	year = 2001;
	month_name = months[month - 1];
}

void Date::showDate1() {								//This line will display the date 1/1/2001 format.
	cout << month << "/" << day << "/" << year << endl;
}

void Date::showDate2() {									//This line will display the date January 1, 2001 format.
	cout << month_name << " " << day << ", " << year << endl;
}

void Date::showDate3() {										//This line will display the date 1 January 2001 format.
	cout << day << " " << month_name << " " << year << endl;
}

void Date::setDate(int m, int d, int y) {			//This line will take month, day, and year as parameters and then it will set the object data member values.
	month = m;
	day = d;
	year = y;
	month_name = months[month - 1];
}



//Date.h
#include<string>
using namespace std;

class Date {
private:
	int month;
	int day;
	int year;
	string month_name;
	static const string months[12];
	
	Date(int m, int d, int y);
	Date();
	void showDate1();
	void showDate2();
	void showDate3();
	void setDate(int m, int d, int y);
};



//Assignment6.cpp
#include<iostream>
using namespace std;
#include "Date.h"
int main()
{
	Date d1;
	Date d2(2, 12, 2010);
	d1.showDate1();
	d2.showDate2();
	d1.setDate(8, 29, 1986);
	d1.showDate3();
	system("pause");
	return 0;
}










closed account (E0p9LyTq)
And the errors are?

Severity Code Description Project File Line Suppression State
Error (active) "Date::Date()" (declared at line 14 of "c:\Users\dylan\Documents\Visual Studio 2015\Projects\Date.h") is inaccessible Hw6.cpp c:\Users\dylan\Documents\Visual Studio 2015\Projects\Assignment6.cpp 7

Severity Code Description Project File Line Suppression State
Error (active) "Date::Date(int m, int d, int y)" (declared at line 13 of "c:\Users\dylan\Documents\Visual Studio 2015\Projects\Date.h") is inaccessible Hw6.cpp c:\Users\dylan\Documents\Visual Studio 2015\Projects\Assignment6.cpp 8



Severity Code Description Project File Line Suppression State
Error (active) function "Date::showDate1" (declared at line 15 of "c:\Users\dylan\Documents\Visual Studio 2015\Projects\Date.h") is inaccessible Hw6.cpp c:\Users\dylan\Documents\Visual Studio 2015\Projects\Assignment6.cpp 9

Severity Code Description Project File Line Suppression State
Error (active) function "Date::showDate2" (declared at line 16 of "c:\Users\dylan\Documents\Visual Studio 2015\Projects\Date.h") is inaccessible Hw6.cpp c:\Users\dylan\Documents\Visual Studio 2015\Projects\Assignment6.cpp 10

Severity Code Description Project File Line Suppression State
Error (active) function "Date::setDate" (declared at line 18 of "c:\Users\dylan\Documents\Visual Studio 2015\Projects\Date.h") is inaccessible Hw6.cpp c:\Users\dylan\Documents\Visual Studio 2015\Projects\Assignment6.cpp 11

Severity Code Description Project File Line Suppression State
Error (active) function "Date::showDate3" (declared at line 17 of "c:\Users\dylan\Documents\Visual Studio 2015\Projects\Date.h") is inaccessible Hw6.cpp c:\Users\dylan\Documents\Visual Studio 2015\Projects\Assignment6.cpp 12
closed account (E0p9LyTq)
ALL your errors state your class is inaccessible in Hw6.cpp. Where is your listing for Hw6.cpp? Or Assignment6.cpp for that matter.

Making a GUESS, I'd say you forgot to include your class header file in those source files.


On actually reading ALL of the code provided, and the errors, your problem is making your constructors and methods private.

Oooops, I was being lazy.
Last edited on
Your functions in the Date.h file are set to private so the client program, Assignment6.cpp, cant access them.

Correct code should be something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

//Date.h
#include<string>
using namespace std;

class Date {
private:
	int month;
	int day;
	int year;
	string month_name;
	static const string months[12];
public:
	Date(int m, int d, int y);
	Date();
	void showDate1();
	void showDate2();
	void showDate3();
	void setDate(int m, int d, int y);
};


My biggest issue is that the showDate methods are private. Main cannot call them. Also I would place the months string array into the .h file and inside the class declaration, make it a public const static variable.

Edit: Also the constructors should not be private.
Last edited on
That was exactly what it was. a simple mistake. i fixed it and now its working. thanks everyone.
glad we could help :D
Topic archived. No new replies allowed.