class within class confusion

Hello

I have been to trying to fix the below code for the last one hour without any success. Could you help me, please? I was trying to compile it in Code::Blocks. Thanks for the help.

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
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

//***********************************************************
class Student
{
    private:
        string name; //John
        int rollNumber;

    private:
        class Date
        {
            public:
                int day;
                string month; // Jan, Feb
                int year;

                public:
                    void readDate();
                    Date(int dummyDay, string dummyMonth, int dummyYear) : dummyDay(day),
                    dummyMonth(month), dummyYear(year)
                    { }
        };


    public:
        void readStudent();
        void printStudent();

};
//************************************************************


int main()
{
    Student student;
    Date date;

    student.readStudent();
    student.printStudent();

    system("pause");
    return 0;
}

//************************************************************

void Student::readStudent()
{
    Student student;

    cout << "enter name: "; cin >> name;
    cout << "enter roll no.: "; cin >> rollNumber;
    readDate();
    Date(int day, string month, int year);
}

//***********************************************************

void Student::printStudent()
{
    cout << "name: " << name << endl;
    cout << "roll number: " << rollNumber << endl;
    cout << "date of birth: " << date.day << "" << date.month << "" << date.year << endl;
}

//************************************************************

void Date::readDate()
{
    int day; string month; int year;

    cout << "enter day: "; cin >> day;
    cout << "enter month: "; cin >> month;
    cout << "enter year: "; cin >> year;
}
void Student::Date::readDate()

readDate();

You can't call this from student because it belongs to the Date class. You would have to create a Date object as a member of student and call the readDate method on that.
Thanks, hanst99.

You can't call this from student because it belongs to the Date class. You would have to create a Date object as a member of student and call the readDate method on that.


The problem is I don't know how to do this. I have made one correction you suggested.

Changed
void Date::readDate()

to
void Student::Date::readDate()

Could you help, please?
Last edited on
The initialisation list for the Date constructor looks wrong

Shouldn't it be

Date(int dummyDay, string dummyMonth, int dummyYear)
: day(dummyDay), month(dummyMonth), year(dummyYear)
{ }

?

The Date class is private and can only be used inside of Student.
mik2718: Yes, you are correct. Thanks. I have fixed it.

I still have few errors to fix.

Errors:
1
2
3
4
5
6
7
8
9
10
11
12
In function 'int main()':|
41|error: 'Date' was not declared in this scope|
41|error: expected ';' before 'date'|
In member function 'void Student::readStudent()':|
58|error: 'readDate' was not declared in this scope|
59|error: expected primary-expression before '(' token|
59|error: expected primary-expression before 'int'|
59|error: expected primary-expression before 'month'|
59|error: expected primary-expression before 'int'|
In member function 'void Student::printStudent()':|
68|error: 'date' was not declared in this scope|
||=== Build finished: 8 errors, 0 warnings ===|
Last edited on
Only read the first error and ignore the rest - fix it - recompile - and then onto the next error.

Date is not accessible in the main scope since it is private to Student.

Suggest you have two separate classes - Student and Date - I don't see why you need nested classes.

Date is not accessible in the main scope since it is private to Student.


Hi

I understand that it's one of the main errors and I also know Date isn't accessible inside the main. But the issue is I don't know what to do to fix it. :(

Yes, there would be different ways to do this problem. But I'm interested to do it this way because it seems the difficult one. :)
Maybe take the
Date date;
thing out of main, create the date when you are in readStudent.

You will have to create the date there, also you will need a Date instance variable in Student

class Student
{
private:
string name; //John
int rollNumber;
Student::Date date; // date instance variable
...


void Student::readStudent()
{
Student student;

cout << "enter name: "; cin >> name;
cout << "enter roll no.: "; cin >> rollNumber;
date.readDate();
}

something like that

Thanks, mik2718.

I have fixed it now. Thanks for the help.
Topic archived. No new replies allowed.