Vectors

Hello, I am still struggling to declare my vectors. I know there is an error around line 185 and I just can't figure out how to solve it. Also, my professor is wanting me to use private functions instead of public. When I try to turn them private, more disaster strikes. Can anyone assist me please? I am just not grasping this. Thanks.

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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include<conio.h>
#include<iostream>
#include<cstdlib>
#include<vector>

using namespace std;

// Base class
class appointment
{
public:
	char*description;
// Declare structure date
	struct date
	{
		int y,m,d;
	};

// Create an object of the structure date
	date dt;
	int time;
public:
	appointment()
	{
	}
	appointment(int d, int m, int y)
	{
// three argument construct for setting the appointment date
		dt.y=y;
		dt.m=m;
		dt.d=d;
	}

// declare a virtual function using three  variable of integers as an argument
	virtual char*occurs_on(int yr, int month, int day){};
};

// Class publicly derived from appointment
class OneTime:public appointment
{
public:
// Declares a default constructor to initialize the object of the class
	OneTime(){}
	OneTime(int d, int m, int y)
	{
		dt.y=y;
		dt.m=m;
		dt.d=d;
	}
// defines the virtual function
	char*occurs_on(int yr, int month, int day)
	{

// check the condition if the day, month and yr is the same value of day
// month and year enter by user.
// Then stores the check value in the description.
		if(dt.d==day&&dt.m==month&&dt.y==yr)
		{
			description="one Time check up";
		}
		else
		{
			description="\0";
		}
// return the description
		return description;
	}
};

// Implements a class Daily for showing the daily appointments
// that's publically derived from the base class appointment

class Daily:public appointment
{
// declares a default constructor to initialize the value of class objects
	Daily(){}
// Declares a parameter constructor using three variables of integer arguments
	Daily(int d, int m, int y)
	{
// sets the appointment 
		dt.y=y;
		dt.m=m;
		dt.d=d;
	}

// Define the virtual function for the derived class
	char*occurs_on(int yr, int month, int day)
	{
// check the condition if the day, month and yr is the same value of day
// month and year enter by user.
// Then stores the check value in the description.
		if(dt.d==day&&dt.m==month&&dt.y==yr)
		{
			description="daily appointment";
		}
		else
		{
			description="\0";
		}
// returns the value of the description
		return description;
	}
};

// Implements class Weekly for showing the weekly appointments
// Class is derived from base class appointment

class Weekly:public appointment
{
public:
// Declares the default constructor to initialize teh objects of the class
	Weekly(){}

// Declares parameters of constructo taking three variables of integers for arguments
	Weekly(int d, int m, int y)
	{
		dt.y=y;
		dt.m=m;
		dt.d=d;
	}

// checks the type of appointment by comparing the date
	char*occurs_on(int yr, int month, int day)
	{
// declares a variable of integer data type
		int b;
// check the condition if the day, month and yr is the same value of day
// month and year enter by user.
// Then stores the check value in the description.
		if(dt.d==day&&dt.m==month&&dt.y==yr)
		{
			description="weekly appointment";
		}
		else
		{
			description="\0";
		}
		return description;
	}
};
 // Implement a class monthly for showing the monthly appointmnet which is 
// publically derived from the base class appointment

class Monthly:public appointment
{
public:

// Declares a default constructor to initialize the objects of the class
	Monthly(){}
	Monthly(int d, int m, int y)
	{
// constructor set the appointment date
		dt.y=y;
		dt.m=m;
		dt.d=d;
	}
// checks the type of appointment
	char*occurs_on(int yr,int month,int day)
	{

// check the condition if the day, month and yr is the same value of day
// month and year enter by user.
// Then stores the check value in the description.
		if(dt.d==day&&dt.m==month&&dt.y==yr)
		{
			description="monthly appointment";
		}
		else
		{
			description="\0";
		}
		return description;
	}
};

// main function of the program
int main()
{

// declares four variable integer data types to display day, month and year
	int x;
	int dy,mn,ye;
	char*ar[4];

// vector of base class appointment
	vector<app> app(6);

// creates object of derived class OneTime and assigns the value
	app[0]=new OneTime(8,28,2013);
// creates object of derived class daily and show daily appointments and assigns value in it
	app[1]=new Daily(8,29,2013);
// creates object of the derived class weekly and shows appointments and assigns value in it
	app[2]=new Weekly(9,1,2013);
// creates object of derived class monthly and show monthly appointments and assigns value in it
	app[3]=new Monthly(p,2,2013);
// creates object of derived class OneTime and assigns the value in it
	app[4]=new OneTime(9,2,2013);
// creates an object of the derived class Monthly it show the monthly appointments and assigns value in it
	app[5]=new Monthly(9,2,2013);

// Enters the value of day, month and year from the user
	cout<<"enter day\t";
	cin>>dy;
	cout<<"enter month\t";
	cin>>mn;
	cout<<"enter year\t";
	cin>>ye;
	for(int i-0;i<6;i++)
	{
// calls the virtual function and stores values of day, month and year in the array
		ar[i]=app[i]->occurs_on(ye,mn,dy);
	}
// declares a variable of integer data type intialized at 0
	int i=0;
	while(i<4)
	{
		if(ar[i]!="\0")
		{
			cout<<"ar["<<i<<"]="<<ar[i];
		}
		i++;
	}
	getch();
	return 0;
}
We'd like to know what is the error.
But anyways that line of code may be translated to something like app app[6] and you cannot have an array named as its own type.

Maybe you meant vector<appointment*> app but you also need to clean up the pointers on your own.
Last edited on
Confirmed, the problem is that:
vector<app> app(6); should be vector<appointment*> app;
Okay, so I made some of the adjustments and took out the b integer and the x integer cuz they really didn't do anything. Now I get the error; main.cpp(51): error C4716: 'appointment::occurs_on' : must return a value. This is the only error now showing. Below is the updated code.
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include<conio.h>
#include<iostream>
#include<cstdlib>
#include<vector>

using namespace std;

// Base class
class appointment
{
public:
	char*description;
// Declare structure date
	struct date
	{
		int y,m,d;
	};

// Create an object of the structure date
	date dt;
	int time;
public:
	appointment()
	{
	}
	appointment(int d, int m, int y)
	{
// three argument construct for setting the appointment date
		dt.y=y;
		dt.m=m;
		dt.d=d;
	}

// declare a virtual function using three  variable of integers as an argument
	virtual char*occurs_on(int yr,int month,int day){};
};

// Class publicly derived from appointment
class OneTime:public appointment
{
public:OneTime(){}

// Declares a default constructor to initialize the object of the class
	OneTime(int d, int m, int y)
	{
		dt.y=y;
		dt.m=m;
		dt.d=d;
	}
// defines the virtual function
	char*occurs_on(int yr, int month, int day)
	{

// check the condition if the day, month and yr is the same value of day
// month and year enter by user.
// Then stores the check value in the description.
		if(dt.d==day&&dt.m==month&&dt.y==yr)
		{
			description="one Time check up";
		}
		else
		{
			description="\0";
		}
// return the description
		return description;
	}
};

// Implements a class Daily for showing the daily appointments
// that's publically derived from the base class appointment

class Daily:public appointment
{
public:
// declares a default constructor to initialize the value of class objects
	Daily(){}
// Declares a parameter constructor using three variables of integer arguments
	Daily(int d, int m, int y)
	{
// sets the appointment 
		dt.y=y;
		dt.m=m;
		dt.d=d;
	}

// Define the virtual function for the derived class
	char*occurs_on(int yr, int month, int day)
	{
// check the condition if the day, month and yr is the same value of day
// month and year enter by user.
// Then stores the check value in the description.
		if(dt.d==day&&dt.m==month&&dt.y==yr)
		{
			description="daily appointment";
		}
		else
		{
			description="\0";
		}
// returns the value of the description
		return description;
	}
};

// Implements class Weekly for showing the weekly appointments
// Class is derived from base class appointment

class Weekly:public appointment
{
public:
// Declares the default constructor to initialize teh objects of the class
	Weekly(){}

// Declares parameters of constructo taking three variables of integers for arguments
	Weekly(int d, int m, int y)
	{
		dt.y=y;
		dt.m=m;
		dt.d=d;
	}

// checks the type of appointment by comparing the date
	char*occurs_on(int yr, int month, int day)
	{

// check the condition if the day, month and yr is the same value of day
// month and year enter by user.
// Then stores the check value in the description.
		if(dt.d==day&&dt.m==month&&dt.y==yr)
		{
			description="weekly appointment";
		}
		else
		{
			description="\0";
		}
		return description;
	}
};
 // Implement a class monthly for showing the monthly appointmnet which is 
// publically derived from the base class appointment

class Monthly:public appointment
{
public:	Monthly(){}

// Declares a default constructor to initialize the objects of the class

	Monthly(int d, int m, int y)
	{
// constructor set the appointment date
		dt.y=y;
		dt.m=m;
		dt.d=d;
	}
// checks the type of appointment
	char*occurs_on(int yr,int month,int day)
	{

// check the condition if the day, month and yr is the same value of day
// month and year enter by user.
// Then stores the check value in the description.
		if(dt.d==day&&dt.m==month&&dt.y==yr)
		{
			description="monthly appointment";
		}
		else
		{
			description="\0";
		}
		return description;
	}
};

// main function of the program
int main()
{

// declares four variable integer data types to display day, month and year

	int dy,mn,ye;
	char*ar[4];

// vector of base class appointment
	vector<appointment*> app(6);

// creates object of derived class OneTime and assigns the value
	app[0]=new OneTime(8,28,2013);
// creates object of derived class daily and show daily appointments and assigns value in it
	app[1]=new Daily(8,29,2013);
// creates object of the derived class weekly and shows appointments and assigns value in it
	app[2]=new Weekly(9,1,2013);
// creates object of derived class monthly and show monthly appointments and assigns value in it
	app[3]=new Monthly(9,2,2013);
// creates object of derived class OneTime and assigns the value in it
	app[4]=new OneTime(9,2,2013);
// creates an object of the derived class Monthly it show the monthly appointments and assigns value in it
	app[5]=new Monthly(9,2,2013);

// Enters the value of day, month and year from the user
	cout<<"enter day\t";
	cin>>dy;
	cout<<"enter month\t";
	cin>>mn;
	cout<<"enter year\t";
	cin>>ye;
	for(int i=0;i<6;i++)
	{
// calls the virtual function and stores values of day, month and year in the array
		ar[i]=app[i]->occurs_on(ye,mn,dy);
	}
// declares a variable of integer data type intialized at 0
	int i=0;
	while(i<4)
	{
		if(ar[i]!="\0")
		{
			cout<<"ar["<<i<<"]="<<ar[i];
		}
		i++;
	}
	_getch();
	return 0;
}
That's simple:

You're saying this function is able to return a char*:
virtual char*occurs_on(int yr,int month,int day){};
But you're not returning anything:
virtual char*occurs_on(int yr,int month,int day){};// <--- !!!

The workarounds are:

Making it "pure", requiring derived classes to redefine it:
virtual char*occurs_on(int yr,int month,int day) = 0;
Or returning 0:
virtual char*occurs_on(int yr,int month,int day){return 0;}

As I see you're using virtual classes, you may want to use a virtual destructor to avoid any kind of memory leaks:

 
virtual ~appointment() {} // somewhere inside the "class appointment{}" block 


Also you're leaking memory from main:
After using std::vector<appointment*>, you should free its elements one by one:
this goes where _getch() is at (basically, after you finished to use app)
1
2
3
4
5
6
for(unsigned int i = 0; i < app.size(); ++i)
{
    delete app[i];
}
app.clear();
_getch();


...or use a smart pointer/unique pointer.
Last edited on
Thanks, I made the suggested changes and am now getting an error message, Run-time Check Failure #2 - Stack around the variable 'ye' was corrupted. At this point I am absolutely clueless.
1
2
3
4
if(ar[i]!="\0")
{
	cout<<"ar["<<i<<"]="<<ar[i];
}

You should change this to
1
2
3
4
if(ar[i]!=0)
{
	cout<<"ar["<<i<<"]="<<ar[i];
}


Note, they are not the same.
The first one is of type char*, and is an actual address (It is NOT 0!).
The second one is of type int (converted to char*), and is not an address (This one IS 0!).
Basically you kept on reading/writing/accessing memory who wasn't initialized.

Also here:

1
2
3
4
for(int i=0;i<6;i++)
{
	ar[i]=app[i]->occurs_on(ye,mn,dy);
}

will attempt to access ar[4] and ar[5], which you should not access as they are out of bounds.
You should resize ar to 6 elements, or use a std::vector<char*> with the same size of app.

EDIT:
For your learning purpose, the crash happened because you obviously accessed data you shouldn't have.
Last edited on
Thanks EssGeEich. I appreciate the help.
Feel free to re-reply in case you need more help making things private (You shouldn't have big issues, but just in case...)
Topic archived. No new replies allowed.