Hi there. I am getting the errors below when I am trying to compile this code using DEV C++.

[Linker error] undefined reference to 'CheckLeapYear'(int)
[Linker error] undefined reference to 'CalcDayNumOfYear'(int)
[Linker error] undefined reference to 'CheckLeapYear'(int)
Id returned 1 exit status

Please help. Below I have included my coding

#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>
using namespace std;

int GetDate (int&, int&, int&); //function prototype
int CheckLeapYear(int);
int CalcDayNumOfYear(int, int, int&);

int main()
{

         int day;
         int month;
         int year;
         int dayOfPreviousMonth=0;
         int dayNumOfYear;
         GetDate (day, month, year);
         CheckLeapYear(year);
         CalcDayNumOfYear(dayOfPreviousMonth, day, dayNumOfYear);
         cout <<"It is day" <<dayNumOfYear<< "of the year."<<endl;
}
         int GetDate(int& day, int& month, int& year)
         {
         cout<<"Enter a date: (dd/mm/yyyy)";
         cin>>day>>month>>year;
         if (day < 1 || day > 31)
         {
                    cout<<"Day entered is invalid"<<endl;
                    }
                    if (month < 1 || month > 12)
                    {
                    cout<<"Month entered is invalid"<<endl;
         }
         int CheckLeapYear(year);
                    {            
                                 if(((year % 100 == 0)&&(year % 400 == 0)) || ((year % 100 != 0) && (year % 4 == 0)))
                                 return true;

                                 else 
                                     return false;

                                     }
                                     }                              

         int CalcDayOfYear(int dayNumber, int month, int year, int dayOfPreviousMonth, int day, int& dayNumOfYear)
         {
                                         if (year != CheckLeapYear (year))
                                         {
                                         switch (month)
                                         {
                                                case 1: dayNumber = day;
                                                break;

                                                case 2: dayNumber = 31+day;
                                                break;

                                                case 3: dayNumber = 59+day;
                                                break;

                                                case 4: dayNumber = 90+day;
                                                break;

                                                case 5: dayNumber = 120+day;
                                                break;

                                                case 6: dayNumber = 151+day;
                                                break;

                                                case 7: dayNumber = 181+day;
                                                break;

                                                case 8: dayNumber = 212+day;
                                                break;

                                                case 9: dayNumber = 243+day;
                                                break;

                                                case 10: dayNumber = 273+day;
                                                break;

                                                case 11: dayNumber = 304+day;
                                                break;
                                                case 12: dayNumber = 334+day;
                                                break;

                                                default:;
                                                }
                                                }
                                                else
                                                switch (month)
                                                {
                                                   case 1: dayNumber = day;
                                                   break;

                                                   case 2: dayNumber = 31+day;
                                                   break;

                                                   case 3: dayNumber = 60+day;
                                                   break;

                                                   case 4: dayNumber = 91+day;
                                                   break;

                                                   case 5: dayNumber = 121+day;
                                                   break;

                                                   case 6: dayNumber = 152+day;
                                                   break;

                                                   case 7: dayNumber = 182+day;
                                                   break;

                                                   case 8: dayNumber = 213+day;
                                                   break;

                                                   case 9: dayNumber = 244+day;
                                                   break;

                                                   case 10: dayNumber = 274+day;
                                                   break;

                                                   case 11: dayNumber = 305+day;
                                                   break;

                                                   case 12: dayNumber = 335+day;
                                                   break;
                                                   default:;

                                                   }

}

Well, for starters, if you'd enclosed your code in code tags it would have been a bit easier to read. But anyway, here are the problems I spotted in your code: (Apologies if there are any I've missed!)

1. Unless I'm mistaken there appears to be a closing brace '}' missing at the end of the GetDate function

2. GetDate is supposed to return an int, but it doesn't actually return anything, so you either need to make it return something or change its return type to void, otherwise you'll get another error later on.

3. Look at your CheckLeapYear function definition:
e.g.

int CheckLeapYear(year);
{
	if(((year % 100 == 0)&&(year % 400 == 0)) || ((year % 100 != 0) && (year % 4 == 0)))
		return true;
	else
		return false;
}

The parameter list should be 'int year' not 'year', the return type of the function should really be bool as you are returning true/false. Also you've stuck a semi-colon at the end of the line, which completely skips the function body....Doh!

4. Immediately after the CheckLeapYear function definition there is an extra closing brace '}'. This is probably the one that was missing from the end of the GetDate function! This is part of your reported problem! More on this later!

5. At the top of your code you have declared/prototyped a function called 'CalcDayNumOfYear', which returns an int and takes three int parameters, yet towards the end of your code, you've defined a function called 'CalcDayOfYear', which returns an int and takes 6 int parameters. This is another part of your reported problem!

Because the function names, signatures and parameter lists don't match-up, the compiler/linker is unable to find a definition for the declared/prototyped function (CalcDayNumOfYear) and throws an undefined reference error.

6. Finally, you've declared that the 'CalcDayOfYear' function should return an int, but it doesn't actually return anything, so you need to either return something or change the functions return type to void instead!


When you get an undefined reference error, it generally means one of two things:
1. You've included a header file for a library, but the compiler/linker cannot find the associated .lib or .so file (this only applies when using 3rd party libraries and definitely isn't the problem in your code!)
or:
2. You've prototyped a function, but not provided an implementation of the function. (This is what happened in your case!)

Your cock-up with the closing braces is what caused the compiler to be unable to find the definition of 'CheckLeapYear'. Because of the mis-placed braces, the definition of 'CheckLeapYear' was essentially embedded inside GetDate (which is illegal in C++ anyway, you can't declare or define functions inside other functions).

Similarly the error regarding CalcDayNumOfYear is because you prototyped a function called 'CalcDayNumOfYear' but implemented a function called 'CalcDayOfYear', so the compiler/linker is still looking for an implementation for 'CalcDayNumOfYear'!


Anyway, those are the most obvious problems I spotted in your code. Apologies if there are any other problems I've missed. Your post would have been a lot easier to read if your code was inside code tags and properly indented!

Please enclose your code with code tags in future posts!

Cheers for now.
Jas.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.