### 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.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225`` ``````#include #include #include #include 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(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["<
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.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225`` ``````#include #include #include #include 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 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["<
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)
 ``123456`` ``````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.
 ``1234`` ``````if(ar[i]!="\0") { cout<<"ar["<

You should change this to
 ``1234`` ``````if(ar[i]!=0) { cout<<"ar["<

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:

 ``1234`` ``````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.