HELP WITH OBJECTS

I would like to know how to call the methods of the class.
I have an object call v which is an array and I don't how to call the methods
of the class. The error is here:
v.readDates(v[a]);

Thank you.


#include "Date.h"
#include <iostream>
using namespace std;

int main(){
int a;
cout << " HOW MANY DATES DO YOU HAVE? " << endl;
cin >> a;
Date v[a];
v.readDates(v[a]);
v.sortDates(v[a]);
v.showDates (v[a]);
return 0;
}
Last edited on
v is an array of objects so you will have to call the methods on each object.

maybe something like:

1
2
3
4
5
6
for( int i = 0; i < a; ++i )
{
    v[i].readDates(v[num]);
    v[i].sortDates(v[num]);
    v[i].showDates (v[num]);
}


Though the parameter being passed is questionable can we see the class?
YES OF COURSE: The number format is:
YYYYMMDD EXAMPLE:
20141130


void Data::readDates(Data [a]){
int a_date;
for (int i=0;i<num;i++){
cout << "ENTER A DATE:" << endl;
cin >> a_date;
int day=v[i].days(a_data);
int year=v[i].years(a_data);
int month=v[i].months(a_data);
while (! isCorrect (day,month,year)){
cin >> a_date;
day=v[i].days(a_data);
year=v[i].years(a_data);
int month=v[i].months(a_data);
}
}
}
Last edited on
void Data::readDates(Data [a]){???

I think you are not understanding how functions/parameters work. I would suggest learning this before moving onto OOP(object-oriented-programming).
http://www.cplusplus.com/doc/tutorial/functions/
http://www.learncpp.com/ -- chapter 7

Also you have a questionable design.


You have a class like this:

1
2
3
4
5
6
7
8
class Data
{
    public:
        Data( const int value );
        void readData( Data d[10] ); //why?
    private:
        int value;
};


and then you are trying to read in all the data for 10 items:

1
2
3
4
5
6
7
8
//int main

Data data[10]; //with correct constructor

for( int i = 0; i < 10; ++i )
{
    data[i].readData(data);
}



I would suggest either

1) changing the read function to read in for the single object (this):
void readData( void ); and calling the read function individually
1
2
3
4
for( int i = 0; i < 10; ++i )
{
    data[i].readData();
}


2) make the private info an array and create one object.
1
2
3
4
5
6
7
8
class
{
    public:
        Data( const int values[10] );
        void readData( void );
    private:
        int value[10];
};
then call the read data once.
1
2
3
//int main
Data data; //well with correct constructor
data.readData();
Last edited on
Topic archived. No new replies allowed.