undefined reference to

Hey guys first off please bare with me the code is quite long but I will note that the error occurs in the initAge function so most of the code you can ignore its the initAge function that is giving me the problems,it is telling me I have an undefined reference,I'm not sure why I'm getting this error

I have been working on a calender project for the last 3 hours and have can't seem to figure this out I've never really experienced this issue before


obj\Release\main.o:main.cpp:(.text.startup+0x251)||undefined reference to `Dates::initAge(Dates::Date&)'|



main.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
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
81
82

#include <iostream>
#include <Date.h>
#include <stdlib.h>

using namespace std;

int main()
{
    Dates::Date today(3,Dates::MAR,Year(2018));
    int months = Dates::nextDecember(today);

    cout << today.getMonth() << endl;
    cout << months << " months until December" << endl;

    int day;
    int monthInt;
    int yearInt;
    int choice;

    cout << "enter the day of the month" << endl;
    cin >> day;

    cout << "enter the month of the year" << endl;
    cin >> monthInt;
    Dates::Month month = Dates::Month(+monthInt);

    cout << "enter the year" << endl;
    cin >> yearInt;
    Year year(yearInt);

    Dates::Date todaysDate(day,month,year);
    Dates::initAge(todaysDate);

    while(true)
    {

        cout << "press 1 to skip a day" <<
             "press 2 to skip a month" <<
             "press 3 to skip a year" <<
             "press 4 to display todays date" <<
             "press 5 to quit" << endl;

        cin >> choice;

        switch(choice)
        {

        case 1:
            Dates::nextDay(todaysDate);
            break;
        case 2:
            Dates::nextMonth(todaysDate);
            break;
        case 3:
            Dates::nextYear(todaysDate);
            break;
        case 4:
            system("cls");
            Dates::checkBirthday(todaysDate);
            Dates::checkChristmas(todaysDate);
            Dates::checkNewYears(todaysDate);
            cout << todaysDate.getDay() << "/"  << todaysDate.getMonth() <<
                 "/"  <<  todaysDate.getYear().y << endl;
            break;
        case 5:
            break;
        case 6:
            Dates::previousDay(todaysDate);
            break;
        case 7:
            Dates::previousMonth(todaysDate);
            break;
        case 8:
            Dates::previousYear(todaysDate);
            break;
        default:
            cout << "invalid choice please re enter";
        }
    }
}
  


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
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
#ifndef DATE_H
#define DATE_H
#include <Year.h>

namespace Dates
{

enum Month
{

    JAN = 1,
    FEB,
    MAR,
    APR,
    MAY,
    JUN,
    JUL,
    AUG,
    SEP,
    OCT,
    NOV,
    DEC
};

class Date
{
public:
    Date();
    Date(int d,Month m,Year y);
    Date(const Date& other);
    virtual ~Date();
    void setAge(int a);
    int getDay() const;
    Month getMonth() const;
    Year getYear() const;
    void setDay(int day);
    void setMonth(Month month);
    void setYear(Year year);
    void incrementAge(Date& d);

protected:

private:
    int day;
    Month month;
    Year year;
    int age;
};

bool operator==(const Date& one,const Date& two);
void initAge(Date& d);
int nextDecember(const Date& d);
void nextDay(Date& d);
void nextMonth(Date& m);
void nextYear(Date& yr);
void previousDay(Date& d);
void previousMonth(Date& d);
void previousYear(Date& d);
void checkChristmas(Date& d);
void checkBirthday(Date& d);
void checkNewYears(Date& d);

}
#endif // DATE_H

  

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
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219

#include "Date.h"
#include <iostream>

using namespace std;

namespace Dates
{

Date::Date()
{
    day = 1;
    month = JAN;
    Year temp(1970);
    year = temp;
}

Date::Date(int d,Month m,Year y)
    : day(d),month(m),year(y)
{
    age = 0;
}

Date::Date(const Date& other){

  this->day = other.day;
  this->month = other.month;
  this->year = other.year;
}

void Date::setAge(int a){

   age = a;
}

int Date::getDay() const
{
    return day;
}
Month Date::getMonth()const
{

    return month;
}
Year Date::getYear()const
{

    return year;
};

void Date::setDay(int day){

   this->day = day;
}

void Date::setMonth(Month month){

  this->month = month;
}

void Date::setYear(Year year){

  this->year = year;
}

Date::~Date()
{
    //dtor
}

bool operator==(const Date& one,const Date& two)
{

    return one.getDay() == two.getDay() && one.getMonth() == two.getMonth() && one.getYear().y == two.getYear().y;
}

void initDate(Date& d){

//    if(d.getDay() < 19 && d.getMonth() < SEP && d.getYear().y < 1991){
//        d.setAge(-1);
//
//    }
//    if(d.getDay() >= 19 && d.getMonth() >= SEP && d.getYear().y == 1991){
//
//       d.setAge(0);
//    }
////    else{
////    Date tempDate = d;
////    cout << tempDate.getDay() << "/" << tempDate.getMonth() << "/" << tempDate.getYear().y << endl;
////    int tempAge = 0;
////
////    while(tempDate.getYear().y != 1991){
////
////       previousDay(tempDate);
////       if(tempDate.getDay() == 19 && tempDate.getMonth() == SEP){
////         tempAge++;
////         d.setAge(tempAge);
////       }
////    }
////    }

}

int nextDecember(const Date& d){

   int s = d.getMonth();
   int count = 0;
   cout << "debug" << s << endl;

   if(s == DEC){

       return count;
   }
   for(int i = s; i <= DEC; i++){

      count++;

      if(i == DEC){

        return count-1;
      }
   }
}

void nextYear(Date& yr){

  int year = (yr.getYear().y)+1;
  yr.setYear(Year(year));
}

void nextMonth(Date& m){

   Month month = m.getMonth();
   if(month == 12){
     m.setDay(1);
     m.setMonth(JAN);
     nextYear(m);
   }
   else{
   m.setMonth(Month(month+1));
   }
}

void nextDay(Date& d){

   int day = d.getDay();
   day++;
   if(day > 31){

    day = 1;
    nextMonth(d);
   }
   d.setDay(day);
}

void previousDay(Date& d){

   int day = d.getDay();
   day--;
   if(day < 1){

    day = 31;
    previousMonth(d);
   }
   d.setDay(day);
}

void previousMonth(Date& d){

  Month month = d.getMonth();

  if(month == 1){

    d.setDay(31);
    month = DEC;
    previousYear(d);

  }else{

    d.setMonth(Month(month-1));
  }
}

void previousYear(Date& d){

   int yr = d.getYear().y;
   yr--;
   d.setYear(yr);

}

void checkChristmas(Date& d){

 if(d.getDay() == 25 && d.getMonth() == DEC){

     cout << "It's Christmas,Merry Christmas!" << endl;
 }

}
void checkBirthday(Date& d){

if(d.getDay() == 19 && d.getMonth() == SEP){

     cout << "Happy Birthday!" << endl;
 }

}

void checkNewYears(Date& d){

   if(d.getDay() == 1 && d.getMonth() == JAN){

     cout << "HAPPY NEW YEAR!" << endl;
 }

}

}
  


thanks
"undefined reference" means it can't find the definition of the function. If you haven't defined it anywhere that's what you need to do.
Last edited on
closed account (E0p9LyTq)
In Date.h, line 51:
void initAge(Date& d);

In Date.cpp. line 77:
void initDate(Date& d)

-------------

None of the stand-alone functions declared in your header file inside your custom namespace are properly qualified in your cpp file.

For instance, instead of void initAge(Date& d) it must be defined as void Dates::initAge(Date& d)

ALL of your non-class functions need the Dates:: qualifier.

Last edited on
ALL of your non-class functions need the Dates:: qualifier.


All of those functions are being implemented inside the Dates namespace in that source file so they shouldn't need to be fully qualified. They will however need to be fully qualified in the main.cpp since there is no using statements for those functions in that source file.



closed account (E0p9LyTq)
I missed the custom namespace use in the cpp file.
thanks guys yeah it was a declaration mistake I had initDate instead of initAge

for a moment I thought something was wrong with the linker haha

thanks :)
Topic archived. No new replies allowed.