Designing a class of students

Before I say anything, this code is for some homework (probably obvious!), so I'm mainly looking for a push in the right direction.

Anyway, I'm trying to make a student object which also has two attributes that are also objects themselves (date and address). Whenever I try to compile the student class in code::blocks (win7) with g++, it keeps giving me all the same error, undefined reference to 'object::function()' (ie: undefined reference to 'date::setDate()')

I'm guessing I might have a problem with my contructors, but even after reading up again in my textbook, I'm kind of at a standstill trying to wrap my head around the problem. I've included part of student.cpp and didn't include the address files since the errors produced are exactly the same as the errors from the date class.

student.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
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
#include <string>
#include <cstdlib>
#include "student.h"


using namespace std;

student::student(){ //default student constructor
    setGPA();
    setCreditHoursCompleted();
    setAnticipatedCompletionDate();
    setBirthday();
    setHomeLocation();
    setFirstName();
    setLastName();
}
student::student(double a, int b, int c, int d, int e, int f, int g, int h, string i, string j, string k, string l, string m, string n, string o){
    setGPA(a);
    setCreditHoursCompleted(b);
    setAnticipatedCompletionDate(c, d, e);
    setBirthday(f, g, h);
    setHomeLocation(i, j, k, l, m);
    setFirstName(n);
    setLastName(o);
}//student constructor

void student::setGPA(){ //default constructor for the GPA
    student::gpa = 0.00;
}

void student::setGPA(double x){ //sets GPA to the value
    student::gpa = x;
}

void student::setCreditHoursCompleted(){ //default constructor for credit hours
    student::creditHoursCompleted = 0;
}

void student::setCreditHoursCompleted(int x){
    student::creditHoursCompleted = x;
}

void student::setAnticipatedCompletionDate(){ //default constructor for the date of graduation
    student::anticipatedCompletionDate.setDay();
    student::anticipatedCompletionDate.setMonth();
    student::anticipatedCompletionDate.setYear();
}

void student::setAnticipatedCompletionDate(int x, int y, int z){ //sets values of Date
    student::anticipatedCompletionDate.setDay(x);
    student::anticipatedCompletionDate.setMonth(y);
    student::anticipatedCompletionDate.setYear(z);
}


student.h
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
#ifndef STUDENT_H_EXISTS
#define STUDENT_H_EXISTS

#include <iostream>
#include <string>
#include <cstdlib>
#include "date.h"
#include "address.h"

using namespace std;

class student {
    private:
        double gpa;
        int creditHoursCompleted;
        date anticipatedCompletionDate;
        date birthday;
        address homeLocation;
        string firstName;
        string lastName;
    public:
        student();
        student(double a, int b, int c, int d, int e, int f, int g, int h, string i, string j, string k, string l, string m, string n, string o);
        void setGPA();
        void setGPA(double x);
        void setCreditHoursCompleted();
        void setCreditHoursCompleted(int x);
        void setAnticipatedCompletionDate();
        void setAnticipatedCompletionDate(int x, int y, int z);
        void setBirthday();
        void setBirthday(int x, int y, int z);
        void setHomeLocation();
        void setHomeLocation(string x, string y, string z, string w, string p);
        void setFirstName();
        void setFirstName(string x);
        void setLastName();
        void setLastName(string x);
        double getGPA();
        int getCreditHoursCompleted();
        date getAnticipatedCompletionDate();
        date getBirthday();
        address getHomeLocation();
        string getFirstName();
        string getLastName();

};


#endif 

date.h
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
//date header file

#ifndef DATE_H_EXISTS
#define DATE_H_EXISTS



using namespace std;

class date {
    private:
        int day;
        int month;
        int year;
    public:
        date();
        date(int x, int y, int z);
        void setDay();
        void setDay(int x);
        void setMonth();
        void setMonth(int x);
        void setYear();
        void setYear(int x);
        int getDate();
        int getMonth();
        int getYear();

};

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
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
//date source code


#include "date.h"

using namespace std;

date::date(){
    setDay();
    setMonth();
    setYear();
}//default constructor

date::date(int x, int y, int z){
    setDay(x);
    setMonth(y);
    setYear(z);
}

void date::setDay(){
    date::day = 1;
}//sets day if nothing is passed

void date::setDay(int x){
    date::day = x;
}//sets day

void date::setMonth(){
    date::month = 1;
}//sets month if nothing is passed

void date::setMonth(int x){
    date::month = x;
}//sets month

void date::setYear(){
    date::year = 2000;
}//sets year if nothing is passed

void date::setYear(int x){
    date::year = x;
}//sets year

int date::getDate(){
    return date::day;
}

int date::getMonth(){
    return date::month;
}

int date::getYear(){
    return date::year;
}


Thanks in advance
void student::setGPA(){ //default constructor for the GPA

Ouch, I suggest you to read on classes and constructors again. It seems you completely misunderstood what constructors, fields and stuff are.
yeah i just noticed I used the wrong terminology there, sorry about that.
It's not just about terminology. It's that for example, a set method that doesn't take any arguments doesn't even make any sense.
Last edited on
Alright, I see what you mean.
And those undefined reference errors are linker errors - your code is a bit weird, but there are technically no real errors there. How do you compile (with command line? If so, what compiler do you use and what commands do you compile with? If you compile with an IDE, which IDE do you use?)
code::blocks win7 version with g++. I use the build function
Actually I found out the issue; since the files were not included in a project, it was only running the individual file and not referencing the others.

Once I made a project and put them all in there, the errors stopped.

And thanks for pointing out my mis-understanding of constructors. Gonna go over those chapters again before continuing!
Topic archived. No new replies allowed.