Gettin Linker error

I'm new to using functions and trying to write a program to print out a calendar. While compiling in Dev C++ i'm getting a

[linker error] undefined reference to 'printCalendarHead(int&)

Here is the code. Why is this happening?

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>

using namespace std;

void printCalendarHead(int&);
void printCalendar(int&, int);

int main()
{

int currentMonth;
int inputYear;
int daysInMonth;
int dayOfWeek;

currentMonth = 1;

cout << "Input year you would like a calendar for: ";
cin >> inputYear;
cout << endl << endl << "Input day of week January 1st starts on 0-Sunday 6-Saturday: ";
cin >> dayOfWeek;
cout << endl << endl << setw(16) << inputYear << endl << endl << endl;


while(currentMonth <= 12)
{

printCalendarHead(currentMonth);

if(currentMonth == 4 || currentMonth == 6 || currentMonth == 9 || currentMonth == 11)
daysInMonth = 30;
else
{
if(currentMonth = 2)
{
if(inputYear % 4 == 0)
{
daysInMonth = 29;
}

daysInMonth = 28;
}

daysInMonth = 31;

}

printCalendar(dayOfWeek, daysInMonth);

cout << endl << endl;

currentMonth++;

}

return 0;
system("pause");

Last edited on
This means you never gave the 'printCalendarHead' function a body.
There is a body, I didnt post the code because i didnt want my post to be too long. here is the body for the function:

void printCalendarHead(/*in*/ int currentMonth)
{
if(currentMonth == 1)
cout << "January";

if(currentMonth == 2)
cout << "February";

if(currentMonth == 3)
cout << "March";

if(currentMonth == 4)
cout << "April";

if(currentMonth == 5)
cout << "May";

if(currentMonth == 6)
cout << "June";

if(currentMonth == 7)
cout << "July";

if(currentMonth == 8)
cout << "August";

if(currentMonth == 9)
cout << "September";

if(currentMonth == 10)
cout << "October";

if(currentMonth == 11)
cout << "November";

if(currentMonth == 12)
cout << "December";

cout << endl;

cout << setw(2) << "S" << " " << "M" << " " << "T" << " ";
cout << "W" << " " << "T" << " " << "F" << " " << "S" << endl;

cout << "--------------------" << endl;

}
That actually is not the body for that particular function because the parameter list is different. So the compiler treats them as two separate functions.

Note the difference:

1
2
3
void printCalendarHead(int&);   // your prototype

void printCalendarHead(/*in*/ int currentMonth)  // your function body 


The prototype is taking a reference (int&) and your function body is taking a normal int.

Change one of them so they match. I'd lose the reference.
good eye! thanks man. i will give it another shot.
worked a charm. programs not working though. could u take a look at this and tell me where its going wrong? the february month is still showing 31 days instead of 28.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
	if(currentMonth == 4 || currentMonth == 6 || currentMonth == 9 || currentMonth == 11)
			daysInMonth = 30;
		else
			{
				if(currentMonth == 2)
				{
					if(inputYear % 4 == 0)
					{
						daysInMonth = 29;
					}
					
					daysInMonth = 28;
				}

				daysInMonth = 31;

			}
daysInMonth is being set to 28 for February as you might expect.

But look what you're doing after that. Look closely.

Once you see the problem, you may notice you have the same problem with leap years.
Last edited on
haha that worked. placement error. it was automatically setting daysInMonth = 31. But now i'm having the same problem with the february if's.... looks like i did the same thing there too. Thanks again!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
	if(currentMonth == 4 || currentMonth == 6 || currentMonth == 9 || currentMonth == 11)
			daysInMonth = 30;
		else
			{
                daysInMonth = 31;
                
				if(currentMonth == 2)
				{
  	                     daysInMonth = 28;         
					if(inputYear % 4 == 0)
					{
						daysInMonth = 29;
					}
					
				
				}

				

			}
Heres the finished program in case anyone is interested. Thanks for the help Disch.

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
// This program prints a calendar based on user input
// vincent 6/25/09

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>

using namespace std;

void printCalendarHead(int&);
void printCalendar(int&, int);

int main()
{

    int currentMonth;
    int inputYear;
    int daysInMonth;
    int dayOfWeek;

	currentMonth = 1;
	
	cout << "Input year you would like a calendar for: ";
	cin >> inputYear;
	cout << endl << endl << "Input day of week January 1st starts on 0-Sunday 6-Saturday: ";
	cin >> dayOfWeek;
	cout << endl << endl << setw(16) << inputYear << endl << endl << endl;


	while(currentMonth <= 12)
	{

		printCalendarHead(currentMonth);		

		if(currentMonth == 4 || currentMonth == 6 || currentMonth == 9 || currentMonth == 11)
			daysInMonth = 30;
		else
			{
                daysInMonth = 31;
                
				if(currentMonth == 2)
				{
   	                   daysInMonth = 28;
   	                   
					if(inputYear % 4 == 0)
    	
						    daysInMonth = 29;
					
					
				
				}

				

			}

		printCalendar(dayOfWeek, daysInMonth);

		cout << endl << endl;

		currentMonth++;

	}	
    system("pause");
	return 0;
	

}

//****************************************************************************************
// print the heading for each month with null function


void printCalendarHead(/*in*/ int& currentMonth)
{
	if(currentMonth == 1)
		cout << "January";
	
	if(currentMonth == 2)
		cout << "February";
	
	if(currentMonth == 3)
		cout << "March";

	if(currentMonth == 4)
		cout << "April";

	if(currentMonth == 5)
		cout << "May";	

	if(currentMonth == 6)
		cout << "June";

	if(currentMonth == 7)
		cout << "July";

	if(currentMonth == 8)
		cout << "August";

	if(currentMonth == 9)
		cout << "September";

	if(currentMonth == 10)
		cout << "October";

	if(currentMonth == 11)
		cout << "November";

	if(currentMonth == 12)
		cout << "December";

	cout << endl;	

	cout << setw(2) << "S" << " " 
         << setw(2) << "M" << " " 
         << setw(2) << "T" << " "
	     << setw(2) << "W" << " " 
         << setw(2) << "T" << " " 
         << setw(2) << "F" << " " 
         << setw(2) << "S" << endl;

	cout << "--------------------" << endl;

}

//*********************************************************************************************
//printing the calendar

//*loop to print out days and keep track of 
//value to keep track of day of week if(dayOfWeek = 6)
						                 //cout << endl;


void printCalendar(/*inout*/ int& dayOfWeek,
		           /*in*/ int daysInMonth)

{

	int day, dayAlign;

	day = 1;	
	dayAlign = 0;	

	while(day <= daysInMonth)
	{
		while(dayAlign < dayOfWeek)
		{
			cout << "   ";
			dayAlign++;
		}
			
			
		cout << setw(2) << day;
		cout << " ";
		dayOfWeek++;
		day++;
        dayAlign++;

		if(dayOfWeek == 7)
			{
				cout << endl;
				
				dayOfWeek = 0;
			}

	}

}
Topic archived. No new replies allowed.