Trouble with classes

Ok, I'm terrible at programming. I've been reading the two textbooks I have and I feel like I'm getting dumber and dumber. All that being said, I could use some help. The instructions for my program are incredibly vague for the most part. So here's my instructions:

Design a class called Date that has data members to store month, day, and year. The class should have a default constructor and a three-parameter constructor that allows the date to be set at the time a new Date object is created. If the user creates a Date object without passing any arguments, or if any of the values passed are invalid, the default values of 1, 1, 2001 (i.e., January 1, 2001) should be used. Be sure your program only accepts reasonable values for month, day, and year. The day should be between 1 and the number of days in the selected month.

Alright, so what this doesn't mention is that I have to have a .h file for the class, and two .cpp files, one for my main and one for my functions. I have set up all three files and I was told my class was set up correctly. So I guess at the moment I'm having trouble with connecting the three files. I'm getting these errors that I don't know how to fix. It's probably easy but again, i'm terrible at this stuff. So here's my errors:


date.cpp:27: error: ISO C++ forbids declaration of ‘get_date’ with no type
date.cpp:27: error: prototype for ‘int date::get_date(int, int, int)’ does not match any in class ‘date’
./date.h:28: error: candidate is: void date::get_date(int, int, int)
main.cpp: In function ‘int main()’:
main.cpp:22: error: expected unqualified-id before ‘.’ token


And here's my files:

date.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class date
{

        private:

                int month;
                int day;
                int year;

        public:

                date();
                date( int, int, int );
                void get_date( int, int, int );
                void check_date( int, int, int );
                int print_date( int, int, int );

};


date.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
#include <iostream>
#include <cstdlib>
#include "./date.h"

using namespace std;


date::date( int m, int d, int y )
{

        month = m;
        day = d;
        year = y;

}

date::get_date( int m, int d, int y )
{

        cout << "Enter month: ";
        cin >> date.month;
        cout << "Enter day: ";
        cin >> date.day;
        cout << "Enter year: ";
        cin >> date.year;

}


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <cstdlib>
#include "./date.h"

using namespace std;

int main()
{

        int m, d, y;

        date.date( m, d, y );

        return 0;

}


I'm truly trying to understand this stuff so if there's anything at all someone can tell me to let me know what/why what I've done is wrong and any suggestions about how I might want to go about completing this code will be greatly appreciated. I must enforce that I don't expect to have any code written for me. Examples about things could be helpful. Mostly I just want to understand. All help is greatly appreciated. :)

**EDIT**
I just realized the line numbers aren't going to be correct because of how I copy and pasted. I hope this makes sense: date.cpp line 27 is the date::get_date( int m, int d, int y ). date.h line 28 is the void get_date(int, int, int). main.cpp line 22 is the date.date(m, d, y). Obviously my get date functions are messed up, I just don't know how. :)
Last edited on
In Date.cpp you forgot the get_date function return value
1
2
3
4
5
6
7
8
9
10
11
void date::get_date( int m, int d, int y )
{

        cout << "Enter month: ";
        cin >> date.month;
        cout << "Enter day: ";
        cin >> date.day;
        cout << "Enter year: ";
        cin >> date.year;

}
It must be like this.

In main
 
date.date( m, d, y );

this line means nothing. You should declare an object of date type
When you define the member functions you have to mention the return type.

 
int date::get_date( int m, int d, int y )
ohh, for that date.date() part. I thought I had declared an object of date type. Guess not. What would be the proper way to declare that then?
Remove the dot.
You define a date object like this:
date myDate(1,15,2015);
This creates a object called myDate and calls date::date(1,15,2015) to initialize it.

Your methods inside date should be declared differently. For example, get_date() doesn't need to take any parameters since it's job is to prompt the user for the month, day and year, and assign the values to the object. Also, there's no reason to call it get_date() when get() will do. To see this, consider how you'd call it:
1
2
3
4
5
6
date myDate;
myDate.get_date();
if (!mydate.check_date()) {
    cout << "invalid date changed to 1/1/2001\n";
}
mydate.print_date();

You know from the declaration that myDate is a date, so having all the methods include _date in the name is redundant. It's much more natural to write:
1
2
3
4
5
6
date myDate;
myDate.get();
if (!mydate.check()) {
    cout << "invalid date changed to 1/1/2001\n";
}
mydate.print();


So class date should be declared like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class date
{
        private:
                int month;
                int day;
                int year;

        public:
                date();
                date( int m, int d, int y);

                // prompt the user for the date and set month, day and year.
                void get();

                // check the date. Return true if it's valid. Otherwise change it to
                // 1/1/2001 and return false.
                bool check();

                // print the date.
                void print();

};
There seems to still be something wrong with the main.cpp code. I made the suggested changes and that definitely helped, and makes sense (thank you for that) but now the error says:

main.cpp: In function ‘int main()’:
main.cpp:21: error: no matching function for call to ‘date::get()’
./date.h:28: note: candidates are: void date::get(int, int, int)
main.cpp:23: error: no matching function for call to ‘date::check()’
./date.h:29: note: candidates are: bool date::check(int, int, int)
main.cpp:26: error: no matching function for call to ‘date::print()’
./date.h:30: note: candidates are: int date::print(int, int, int)


The only thing I changed in the .h file was removing the unnecessary _date from the names and I made sure I took that out everywhere.
Looks like you still has not removed the function parameters from date.h.
date.cpp:27: error: ISO C++ forbids declaration of ‘get_date’ with no type
date.cpp:27: error: prototype for ‘int date::get_date(int, int, int)’ does not match any in class ‘date’

./date.h:28: error: candidate is: void date::get_date(int, int, int)


when you define a member function from a class to outside the class or different file

you should include its type

1
2
3
4
5
6
7
8
9
type classname::memberfunctionname()  // syntax
{
//body
}

void date::get_date(int d, int m, int y )
{
//body
}





main.cpp: In function ‘int main()’:
main.cpp:22: error: expected unqualified-id before ‘.’ token


1
2
3
4
5
6
7
8
9
10
int main()
{

        int m, d, y;

        date.date( m, d, y );

        return 0;

}


you cannot call the constructor like that you should instantiate or create an object of that class first.
1
2
3
4
int main()
{
    date myDate(m,d,y);
}

or you can also do date(m,d,y) , example:

1
2
3
4
5
date myDate()
{
int m,d,y;
return date(m,d,y); 
}


Last edited on
main.cpp: In function ‘int main()’:
main.cpp:21: error: no matching function for call to ‘date::get()’
./date.h:28: note: candidates are: void date::get(int, int, int)
main.cpp:23: error: no matching function for call to ‘date::check()’
./date.h:29: note: candidates are: bool date::check(int, int, int)
main.cpp:26: error: no matching function for call to ‘date::print()’
./date.h:30: note: candidates are: int date::print(int, int, int)


this error appears because you dont have print(),check(),get() with void(empty) parameter that you are defining.

you should specify its parameter too when defining member functions

Last edited on
Hmm, sorry, I think I'm getting more confused. I've also messed around with my code so much that I may have made it worse. So peter also mentioned making my parameters empty. I've emptied them now, not sure if I emptied them in the right places or not. Here's what I have now:

.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 15 class date
 16 {
 17 
 18         private:
 19 
 20                 int month;
 21                 int day;
 22                 int year;
 23 
 24         public:
 25 
 26                 date();
 27                 void get();
 28                 bool check();
 29                 int print();
 30 
 31 };


functions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 18 date::date( int m, int d, int y )
 19 {
 20 
 21         month = m;
 22         day = d;
 23         year = y;
 24 
 25 }
 26 
 27 void date::get( int m, int d, int y )
 28 {
 29 
 30         cout << "Enter month: ";
 31         cin >> m;
 32         cout << "Enter day: ";
 33         cin >> d;
 34         cout << "Enter year: ";
 35         cin >> y;
 36 
 37 }


main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 17 int main()
 18 {
 19 
 20         int m, d, y;
 21 
 22         date my_date( m, d, y );
 23         my_date.get( m, d, y );
 24 
 25         if ( !my_date.check() )
 26                 cout << "Invalid date changed to 1/1/2001" << endl;
 27 
 28         my_date.print();
 29 
 30         return 0;
 31 
 32 }


oh, and you said I should specify the parameters when defining the functions. Do mean in my .h file or the function file. I've become so used to needing the parameters in functions and function calls that I think I'm confused about when/where to have or not parameters when dealing with these classes. Thank you for all your help by the way. :)
@brosephius

have you read about functions? prototype(declaration) and definitions?
http://www.cplusplus.com/doc/tutorial/functions/

I've become so used to needing the parameters in functions and function calls that I think I'm confused about when/where to have or not parameters when dealing with these classes.


dont confuse, the member function of your class (e.g function inside your class) is the prototype of the function and the definition is when you already write a body of that function

and you said I should specify the parameters when defining the functions.

if you are declaring it as empty function
1
2
3
void get();
        bool check();
        int print();

[/code]
you should also define it as an empty function
1
2
3
4
5
6
7
8
9
10
11
12
void date::get()
{
//body
}
bool date::check()
{
//body
}
int date::print()
{
//print
}


same goes for your constructor
1
2
3
4
5
 date(); 
    date::date()
{
//intialization
}

Last edited on
Ok, so I actually don't need parameters anywhere then. How then do I pass values/references amongst the functions. Or do I not need to because the functions are all part of the same class and they return some value?
. How then do I pass values/references amongst the functions.

if you want that, then declare the functions with parameter/s
1
2
3
 27                 void get();
 28                 bool check();
 29                 int print();


change your code into this
1
2
3
                void get_date( int, int, int ); // you can pass 3 ints by value
                void check_date( int, int, int ); // you can pass 3 ints by value
                int print_date( int, int, int ); // you can pass 3 ints by value 


Or do I not need to because the functions are all part of the same class and they return some value? 

the one advantage of class is you can easily manipulate the data members(variables)
in the class by accessing them from member functions ( so yes you can access the variables direclty from member functions)

example:
your print function
1
2
3
4
5
 void date::print()
{
std::cout << year << "/" << month << "/" << day << '\n'; 
// did you notice? i access your year,month,day variables without passing any //parameter?
}


PS:
dont pass if you dont need to.
Last edited on
Topic archived. No new replies allowed.