What does this error mean?

error LNK2019: unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class DocComponent<float> &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@AAV?$DocComponent@M@@@Z) referenced in function _main
1>C:\Users\Jaise\Documents\Visual Studio 2008\Projects\Tester\Debug\Tester.exe : fatal error LNK1120: 1 unresolved externals
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include <iostream>	// for I/O functions
#include <vector>	// for vector

using namespace std;
template <class DT>
class DocComponent
{
	// class variables - protected
protected:
	DT top;		//the top margin - the distance from the top of the page in inches
	DT left;		// the left margin - where the text starts
	DT right;	// the right margin - where the text ends

	// class methods - public
public:
	/* DocComponent constructor with no parameters */
	DocComponent();

	/* DocComponent constructor 
	 * 1. Margins - top, left, right */
	DocComponent(DT top, DT left, DT right);

	/* DocComponent copy constructor - copies one instance of DocComponent with another */
	DocComponent(const DocComponent<DT> &component);

	/* DocComponent destructor */
	virtual ~DocComponent();

	/* formats the object for display to the user */
	virtual void display(ostream &s);

	/* overloaded ostream << operator - displays the DocComponent object variables wih help from display() */
	friend ostream& operator<<(ostream &s, DocComponent<DT> &obj);

	/* overloaded assignment = operator 
	 * it assigns the values of one DocComponent instance to another */
	DocComponent<DT>& operator=(const DocComponent<DT> &doc);

	/* overloaded comparison > operator
	 *
	 * ALGORITHM:
	 * first compares the 'top' field of obj1 with obj2:
	 *		1.	if obj1.top > obj2.top, returns TRUE 
	 *		2.	if obj1.top < obj2.top, returns FALSE
	 *		3.	if obj1.top = obj2.top, it then compares the 'left' field of obj1 with obj2 to see which is greater:
	 *		
	 *		3a. if obj1.left > obj2.left, returns TRUE
	 *		3b. if obj1.left < obj2.left, returns FALSE
	 *		3c. if obj1.left = obj2.left, returns FALSE */
	friend bool operator> (DocComponent<DT> &obj1, DocComponent<DT> &obj2);

	/* overloaded comparison < operator
	 *
	 * ALGORITHM:
	 * first compares the 'top' field of obj1 with obj2:
	 *		1.	if obj1.top < obj2.top, returns TRUE 
	 *		2.	if obj1.top > obj2.top, returns FALSE
	 *		3.	if obj1.top = obj2.top, it then compares the 'left' field of obj1 with obj2 to see which is greater:
	 *		
	 *		3a.	if obj1.left < obj2.left, returns TRUE
	 *		3b. if obj1.left > obj2.left, returns FALSE
	 *		3c.	if obj1.left = obj2.left, returns FALSE */
	friend bool operator< (DocComponent<DT> &obj1, DocComponent<DT> &obj2);

	/* overloaded comparison >= operator
	 * 
	 * ALGORITHM:
	 * compares the 'top' field of obj1 with obj2:
	 *		1.  if obj1.top >= obj2.top, returns TRUE
	 *		2.  if obj1.top < obj2.top, returns FALSE */
	friend bool operator>= (DocComponent<DT> &obj1, DocComponent<DT> &obj2);

	/* overloaded comparison <= operator
	 * ALGORITHM:
	 * compares the 'top' field of obj1 with obj2:
	 *		1.  if obj1.top <= obj2.top, returns TRUE
	 *		2.  if obj1.top > obj2.top, returns FALSE */
	friend bool operator<= (DocComponent<DT> &obj1, DocComponent<DT> &obj2);

	/* overloaded comparison == operator
	 * ALGORITHM:
	 * compares the 'top' field of obj1 with obj2:
	 *		1.  if obj1.top == obj2.top, then compares the the 'left' field of obj1 with obj2 to see which is greater:
	 *		1a. if obj1.left == obj2.left, returns TRUE
	 *		2.  if the 'top' fields do NOT match, returns FALSE */
	friend bool operator== (DocComponent<DT> &obj1, DocComponent<DT> &obj2);

	/* overloaded comparison != operator
	 * ALGORITHM:
	 * compares the 'top' field of obj1 with obj2: 
	 *		1. returns the NOT of the == operation */
	friend bool operator!= (DocComponent<DT> &obj1, DocComponent<DT> &obj2);
};

DocComponent<DT>::DocComponent()
{
	// initialize default variable values 
	this->top = 0;
	this->left = 0;
	this->right = 0;
}

DocComponent<DT>::DocComponent(DT top, DT left, DT right)
{
	// initialize variables with respectful parameters
	this->top = top;
	this->left = left;
	this->right = right;
}

template <class DT>
DocComponent<DT>::DocComponent(const DocComponent<DT> &component)
{
	// initialize variables with the same values as those of the component object, thus copying them
	this->top = component.top;
	this->left = component.left;
	this->right = component.right;
}

template <class DT>
DocComponent<DT>::~DocComponent()
{
	// do nothing - compiler default
}

template <class DT>
void DocComponent<DT>::display(ostream &s)
{
	// visual formatting
	s << "*DOC_COMPONENT*\n" << 
		"Top Margin (in inches): " << top << "\n" <<
		"Left Margin (in inches): " << left << "\n" <<
		"Right Margin (in inches): " << right << "\n\n";
}
//------------------------------------------------------------------------------------------------------
template <class DT>
ostream& operator<<(ostream &s, DocComponent<DT> &doc)
{
	// output to the user via cout command
	doc.display(s);
	return s;
}
//------------------------------------------------------------------------------------------------------
template <class DT>
DocComponent<DT>& DocComponent<DT>::operator= (const DocComponent<DT> &doc)
{
	this->top = doc.top;
	this->left = doc.left;
	this->right = doc.right;
	return *this;
}
//------------------------------------------------------------------------------------------------------
template <class DT>
bool operator> (DocComponent<DT> &obj1, DocComponent<DT> &obj2)
{
	if (obj1.top > obj2.top)
	{
		// obj1 is greater than obj2
		return true;
	}
	else if (obj1.top < obj2.top)
	{
		// obj1 is less (NOT GREATER) than obj2
		return false;
	}
	else // obj1 and obj2 'left' fields are equal so continue by comparing the 'left' field
	{
		if (obj1.left > obj2.left)
		{
			// obj1 is greater than obj2
			return true;
		}
		else if (obj1.left < obj2.left)
		{
			// obj1 is less (NOT GREATER) than obj2
			return false;
		}
		else 
		{
			// obj1 and obj2 'left' fields are the same, return false because one is NOT greater or less than the other
			return false;
		}
	}
}
//------------------------------------------------------------------------------------------------------
template <class DT>
bool operator< (DocComponent<DT> &obj1, DocComponent<DT> &obj2)
{
	if (obj1.top < obj2.top)
	{
		// obj1 is less than obj2
		return true;
	}
	else if (obj1.top > obj2.top)
	{
		// obj1 is greater (NOT LESS THAN) than obj2
		return false;
	}
	else // obj1 and obj2 'top' fields are equal so continue by comparing the 'left' field
	{
		if (obj1.left < obj2.left)
		{
			// obj1 is less than obj2
			return true;
		}
		else if (obj1.left > obj2.left)
		{
			// obj1 is greater (NOT LESS THAN) than obj2
			return false;
		}
		else // obj1 and obj2 'left' fields are the same, return false because one is NOT greater or less than the other
		{
			return false;
		}
	}
}
//------------------------------------------------------------------------------------------------------
template <class DT>
bool operator>= (DocComponent<DT> &obj1, DocComponent<DT> &obj2)
{
	if (obj1.top >= obj2.top)
	{
		// obj1 is greater than or equal to obj2
		return true;
	}
	else
	{
		// obj1 has to be less than obj2
		return false;
	}
}
//------------------------------------------------------------------------------------------------------
template <class DT>
bool operator<= (DocComponent<DT> &obj1, DocComponent<DT> &obj2)
{
	if (obj1.top <= obj2.top)
	{
		// obj1 is less than or equal to obj2
		return true;
	}
	else
	{
		// obj1 has to greater than obj2
		return false;
	}
}
//------------------------------------------------------------------------------------------------------
template <class DT>
bool operator== (DocComponent<DT> &obj1, DocComponent<DT> &obj2)
{
	if (obj1.top == obj2.top) // first field 'top' is equal, so compares the next field 'left' 
	{
		if (obj1.left == obj2.left)
		{
			// 'left' fields are equal, the objects are equal
			return true;
		}
		else
		{
			// 'left' fields ARE NOT equal, the objects ARE NOT equal
			return false;
		}
	}
	return false; // first field 'top' is NOT equal, so they cannot be equal, false!
}
//------------------------------------------------------------------------------------------------------
template <class DT>
bool operator!= (DocComponent<DT> &obj1, DocComponent<DT> &obj2)
{
	// returns the opposite of the == operation
	return !(obj1 == obj2);
}

void main()
{
	DocComponent<float>* d = new DocComponent<float>(1, 2, 3);
	cout << *d << endl;
	delete d;	d = NULL;
}
Last edited on
Errors relating to Templates can look really ugly.
The error you have can be simplified as follows:

error LNK2019: unresolved external symbol
"basic_ostream & operator<<(basic_ostream &, DocComponent &)"
referenced in function _main


This error is caused by line 277 cout << *d
To use cout on a class in this way your operator << function
on lines 136 - 142 should have been implemented as a friend function
(not a class member function).
I am not sure that I understand. I declared it as a friend on line 33.

I tried moving it around:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class DocComponent
{
	/* overloaded ostream << operator - displays the DocComponent object variables wih help from display() */
	friend ostream& operator<<(ostream &s, DocComponent<DT> &obj);

	// class variables - protected
protected:
	DT top;		//the top margin - the distance from the top of the page in inches
	DT left;		// the left margin - where the text starts
	DT right;	// the right margin - where the text ends

	// class methods - public
public:
	/* DocComponent constructor with no parameters */
	DocComponent();

	/* DocComponent constructor 
	 * 1. Margins - top, left, right */
	DocComponent(DT top, DT left, DT right);


Just moved it to the top of the class, but still getting an error:

MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
1>C:\Users\Jaise\Documents\Visual Studio 2008\Projects\Tester\Debug\Tester.exe : fatal error LNK1120: 1 unresolved externals

I tried changing the linker options also, from console to windows, and still doesnt work. I changed it back and same error. I have no idea.
Last edited on
The problem is your friend declaration. It's not doing what you think. The template type DT doesn't apply to it, as it's not a member of your class, it needs it's own specialisation. You should probably use something like:

1
2
3
4
5
6
7
template <class DT>
class DocComponent
{
	template <typename T>
	friend ostream& operator<<(ostream &s, const DocComponent<T> &obj);

	// ... 



BTW, I've added const as you wouldn't expect serialising your class to change it.
UGH! This thing is a pain. I did all of your modifications and still get the error:

1
2
3
4
5
template <class DT>
class DocComponent
{
	/* overloaded ostream << operator - displays the DocComponent object variables wih help from display() */
	friend ostream& operator<<(ostream &s, const DocComponent<T> &obj);


1
2
3
4
5
6
7
template <class T>
ostream& operator<<(ostream &s, const DocComponent<T> &doc)
{
	// output to the user via cout command
	doc.display(s);
	return s;
}


error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup

sorry man, I don't know why its doing that. It works fine without the templating and blows up when I add it in there. Thanks for the help though =]
Your main should be return an int and not void.
I tried that also. I was told that it doesn't make a difference.

1
2
3
4
5
6
7
8
int main()
{
    
	DocComponent<float>* d = new DocComponent<float>(1, 2, 3);
	cout << *d << endl;
	delete d;	d = NULL;
	return 0;
}


I did that with above code + modified, and still the same error.
Can you post the complete code and the linker error again please. I'm not sure what you have.
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#include <iostream>	// for I/O functions
#include <vector>	// for vector

using namespace std;


template <class DT>
class DocComponent
{
	/* overloaded ostream << operator - displays the DocComponent object variables wih help from display() */
	friend ostream& operator<<(ostream &s, const DocComponent<T> &obj);

	// class variables - protected
protected:
	DT top;		//the top margin - the distance from the top of the page in inches
	DT left;		// the left margin - where the text starts
	DT right;	// the right margin - where the text ends

	// class methods - public
public:
	/* DocComponent constructor with no parameters */
	DocComponent();

	/* DocComponent constructor 
	 * 1. Margins - top, left, right */
	DocComponent(DT top, DT left, DT right);

	/* DocComponent copy constructor - copies one instance of DocComponent with another */
	DocComponent(const DocComponent<DT> &component);

	/* DocComponent destructor */
	virtual ~DocComponent();

	/* formats the object for display to the user */
	virtual void display(ostream &s);

	/* overloaded assignment = operator 
	 * it assigns the values of one DocComponent instance to another */
	DocComponent<DT>& operator=(const DocComponent<DT> &doc);

	friend bool operator> (DocComponent<DT> &obj1, DocComponent<DT> &obj2);

	friend bool operator< (DocComponent<DT> &obj1, DocComponent<DT> &obj2);

	/* overloaded comparison >= operator
	 * 
	 * ALGORITHM:
	 * compares the 'top' field of obj1 with obj2:
	 *		1.  if obj1.top >= obj2.top, returns TRUE
	 *		2.  if obj1.top < obj2.top, returns FALSE */
	friend bool operator>= (DocComponent<DT> &obj1, DocComponent<DT> &obj2);

	/* overloaded comparison <= operator
	 * ALGORITHM:
	 * compares the 'top' field of obj1 with obj2:
	 *		1.  if obj1.top <= obj2.top, returns TRUE
	 *		2.  if obj1.top > obj2.top, returns FALSE */
	friend bool operator<= (DocComponent<DT> &obj1, DocComponent<DT> &obj2);

	/* overloaded comparison == operator
	 * ALGORITHM:
	 * compares the 'top' field of obj1 with obj2:
	 *		1.  if obj1.top == obj2.top, then compares the the 'left' field of obj1 with obj2 to see which is greater:
	 *		1a. if obj1.left == obj2.left, returns TRUE
	 *		2.  if the 'top' fields do NOT match, returns FALSE */
	friend bool operator== (DocComponent<DT> &obj1, DocComponent<DT> &obj2);

	/* overloaded comparison != operator
	 * ALGORITHM:
	 * compares the 'top' field of obj1 with obj2: 
	 *		1. returns the NOT of the == operation */
	friend bool operator!= (DocComponent<DT> &obj1, DocComponent<DT> &obj2);
};
//======================================================================================================
template <class DT>
DocComponent<DT>::DocComponent()
{
	// initialize default variable values 
	this->top = 0;
	this->left = 0;
	this->right = 0;
}
//------------------------------------------------------------------------------------------------------
template <class DT>
DocComponent<DT>::DocComponent(DT top, DT left, DT right)
{
	// initialize variables with respectful parameters
	this->top = top;
	this->left = left;
	this->right = right;
}
//------------------------------------------------------------------------------------------------------
template <class DT>
DocComponent<DT>::DocComponent(const DocComponent<DT> &component)
{
	// initialize variables with the same values as those of the component object, thus copying them
	this->top = component.top;
	this->left = component.left;
	this->right = component.right;
}
//------------------------------------------------------------------------------------------------------
template <class DT>
DocComponent<DT>::~DocComponent()
{
	// do nothing - compiler default
}
//------------------------------------------------------------------------------------------------------
template <class DT>
void DocComponent<DT>::display(ostream &s)
{
	// visual formatting
	s << "*DOC_COMPONENT*\n" << 
		"Top Margin (in inches): " << top << "\n" <<
		"Left Margin (in inches): " << left << "\n" <<
		"Right Margin (in inches): " << right << "\n\n";
}
//------------------------------------------------------------------------------------------------------
template <class T>
ostream& operator<<(ostream &s, const DocComponent<T> &doc)
{
	// output to the user via cout command
	doc.display(s);
	return s;
}
//------------------------------------------------------------------------------------------------------
template <class DT>
DocComponent<DT>& DocComponent<DT>::operator= (const DocComponent<DT> &doc)
{
	this->top = doc.top;
	this->left = doc.left;
	this->right = doc.right;
	return *this;
}
//------------------------------------------------------------------------------------------------------
template <class DT>
bool operator> (DocComponent<DT> &obj1, DocComponent<DT> &obj2)
{
	if (obj1.top > obj2.top)
	{
		// obj1 is greater than obj2
		return true;
	}
	else if (obj1.top < obj2.top)
	{
		// obj1 is less (NOT GREATER) than obj2
		return false;
	}
	else // obj1 and obj2 'left' fields are equal so continue by comparing the 'left' field
	{
		if (obj1.left > obj2.left)
		{
			// obj1 is greater than obj2
			return true;
		}
		else if (obj1.left < obj2.left)
		{
			// obj1 is less (NOT GREATER) than obj2
			return false;
		}
		else 
		{
			// obj1 and obj2 'left' fields are the same, return false because one is NOT greater or less than the other
			return false;
		}
	}
}
//------------------------------------------------------------------------------------------------------
template <class DT>
bool operator< (DocComponent<DT> &obj1, DocComponent<DT> &obj2)
{
	if (obj1.top < obj2.top)
	{
		// obj1 is less than obj2
		return true;
	}
	else if (obj1.top > obj2.top)
	{
		// obj1 is greater (NOT LESS THAN) than obj2
		return false;
	}
	else // obj1 and obj2 'top' fields are equal so continue by comparing the 'left' field
	{
		if (obj1.left < obj2.left)
		{
			// obj1 is less than obj2
			return true;
		}
		else if (obj1.left > obj2.left)
		{
			// obj1 is greater (NOT LESS THAN) than obj2
			return false;
		}
		else // obj1 and obj2 'left' fields are the same, return false because one is NOT greater or less than the other
		{
			return false;
		}
	}
}
//------------------------------------------------------------------------------------------------------
template <class DT>
bool operator>= (DocComponent<DT> &obj1, DocComponent<DT> &obj2)
{
	if (obj1.top >= obj2.top)
	{
		// obj1 is greater than or equal to obj2
		return true;
	}
	else
	{
		// obj1 has to be less than obj2
		return false;
	}
}
//------------------------------------------------------------------------------------------------------
template <class DT>
bool operator<= (DocComponent<DT> &obj1, DocComponent<DT> &obj2)
{
	if (obj1.top <= obj2.top)
	{
		// obj1 is less than or equal to obj2
		return true;
	}
	else
	{
		// obj1 has to greater than obj2
		return false;
	}
}
//------------------------------------------------------------------------------------------------------
template <class DT>
bool operator== (DocComponent<DT> &obj1, DocComponent<DT> &obj2)
{
	if (obj1.top == obj2.top) // first field 'top' is equal, so compares the next field 'left' 
	{
		if (obj1.left == obj2.left)
		{
			// 'left' fields are equal, the objects are equal
			return true;
		}
		else
		{
			// 'left' fields ARE NOT equal, the objects ARE NOT equal
			return false;
		}
	}
	return false; // first field 'top' is NOT equal, so they cannot be equal, false!
}
//------------------------------------------------------------------------------------------------------
template <class DT>
bool operator!= (DocComponent<DT> &obj1, DocComponent<DT> &obj2)
{
	// returns the opposite of the == operation
	return !(obj1 == obj2);
}

void main()
{
	DocComponent<float>* d = new DocComponent<float>(1, 2, 3);
	cout << *d << endl;
	delete d;	d = NULL;
}
Last edited on
1>Linking...
1>MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
1>C:\Users\Jaise\Documents\Visual Studio 2008\Projects\Tester\Debug\Tester.exe : fatal error LNK1120: 1 unresolved externals
If you mess about and switch your program between Console and
Windows types. You have to make sure that you do it properly..

Try this code on for size.
Check out:
Line 11
Line 35
Line 109
Lines 118 - 124

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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#include <iostream>	// for I/O functions
#include <vector>	// for vector

using namespace std;


template <class DT>
class DocComponent
{
  /* overloaded ostream << operator - displays the DocComponent object variables with help from display() */
   template <typename T> friend ostream& operator<< (ostream &s, const DocComponent<T> &obj);

  // class variables - protected
protected:
  DT top;		//the top margin - the distance from the top of the page in inches
  DT left;		// the left margin - where the text starts
  DT right;	// the right margin - where the text ends

  // class methods - public
public:
  /* DocComponent constructor with no parameters */
  DocComponent();

  /* DocComponent constructor 
  * 1. Margins - top, left, right */
  DocComponent(DT top, DT left, DT right);

  /* DocComponent copy constructor - copies one instance of DocComponent with another */
  DocComponent(const DocComponent<DT> &component);

  /* DocComponent destructor */
  virtual ~DocComponent();

  /* formats the object for display to the user */
  virtual void display(ostream &s) const;

  /* overloaded assignment = operator 
  * it assigns the values of one DocComponent instance to another */
  DocComponent<DT>& operator=(const DocComponent<DT> &doc);

  friend bool operator> (DocComponent<DT> &obj1, DocComponent<DT> &obj2);

  friend bool operator< (DocComponent<DT> &obj1, DocComponent<DT> &obj2);

  /* overloaded comparison >= operator
  * 
  * ALGORITHM:
  * compares the 'top' field of obj1 with obj2:
  *		1.  if obj1.top >= obj2.top, returns TRUE
  *		2.  if obj1.top < obj2.top, returns FALSE */
  friend bool operator>= (DocComponent<DT> &obj1, DocComponent<DT> &obj2);

  /* overloaded comparison <= operator
  * ALGORITHM:
  * compares the 'top' field of obj1 with obj2:
  *		1.  if obj1.top <= obj2.top, returns TRUE
  *		2.  if obj1.top > obj2.top, returns FALSE */
  friend bool operator<= (DocComponent<DT> &obj1, DocComponent<DT> &obj2);

  /* overloaded comparison == operator
  * ALGORITHM:
  * compares the 'top' field of obj1 with obj2:
  *		1.  if obj1.top == obj2.top, then compares the the 'left' field of obj1 with obj2 to see which is greater:
  *		1a. if obj1.left == obj2.left, returns TRUE
  *		2.  if the 'top' fields do NOT match, returns FALSE */
  friend bool operator== (DocComponent<DT> &obj1, DocComponent<DT> &obj2);

  /* overloaded comparison != operator
  * ALGORITHM:
  * compares the 'top' field of obj1 with obj2: 
  *		1. returns the NOT of the == operation */
  friend bool operator!= (DocComponent<DT> &obj1, DocComponent<DT> &obj2);
};
//======================================================================================================
template <class DT>
DocComponent<DT>::DocComponent()
{
  // initialize default variable values 
  this->top = 0;
  this->left = 0;
  this->right = 0;
}
//------------------------------------------------------------------------------------------------------
template <class DT>
DocComponent<DT>::DocComponent(DT top, DT left, DT right)
{
  // initialize variables with respectful parameters
  this->top = top;
  this->left = left;
  this->right = right;
}
//------------------------------------------------------------------------------------------------------
template <class DT>
DocComponent<DT>::DocComponent(const DocComponent<DT> &component)
{
  // initialize variables with the same values as those of the component object, thus copying them
  this->top = component.top;
  this->left = component.left;
  this->right = component.right;
}
//------------------------------------------------------------------------------------------------------
template <class DT>
DocComponent<DT>::~DocComponent()
{
  // do nothing - compiler default
}
//------------------------------------------------------------------------------------------------------
template <class DT>
void DocComponent<DT>::display(ostream &s) const
{
  // visual formatting
  s << "*DOC_COMPONENT*\n" << 
    "Top Margin (in inches): " << top << "\n" <<
    "Left Margin (in inches): " << left << "\n" <<
    "Right Margin (in inches): " << right << "\n\n";
}
//------------------------------------------------------------------------------------------------------
template <typename T>
ostream& operator<<(ostream &s, const DocComponent<T> &doc)
{
  // output to the user via cout command
  doc.display(s);
  return s;
}
//------------------------------------------------------------------------------------------------------
template <class DT>
DocComponent<DT>& DocComponent<DT>::operator= (const DocComponent<DT> &doc)
{
  this->top = doc.top;
  this->left = doc.left;
  this->right = doc.right;
  return *this;
}
//------------------------------------------------------------------------------------------------------
template <class DT>
bool operator> (DocComponent<DT> &obj1, DocComponent<DT> &obj2)
{
  if (obj1.top > obj2.top)
  {
    // obj1 is greater than obj2
    return true;
  }
  else if (obj1.top < obj2.top)
  {
    // obj1 is less (NOT GREATER) than obj2
    return false;
  }
  else // obj1 and obj2 'left' fields are equal so continue by comparing the 'left' field
  {
    if (obj1.left > obj2.left)
    {
      // obj1 is greater than obj2
      return true;
    }
    else if (obj1.left < obj2.left)
    {
      // obj1 is less (NOT GREATER) than obj2
      return false;
    }
    else 
    {
      // obj1 and obj2 'left' fields are the same, return false because one is NOT greater or less than the other
      return false;
    }
  }
}
//------------------------------------------------------------------------------------------------------
template <class DT>
bool operator< (DocComponent<DT> &obj1, DocComponent<DT> &obj2)
{
  if (obj1.top < obj2.top)
  {
    // obj1 is less than obj2
    return true;
  }
  else if (obj1.top > obj2.top)
  {
    // obj1 is greater (NOT LESS THAN) than obj2
    return false;
  }
  else // obj1 and obj2 'top' fields are equal so continue by comparing the 'left' field
  {
    if (obj1.left < obj2.left)
    {
      // obj1 is less than obj2
      return true;
    }
    else if (obj1.left > obj2.left)
    {
      // obj1 is greater (NOT LESS THAN) than obj2
      return false;
    }
    else // obj1 and obj2 'left' fields are the same, return false because one is NOT greater or less than the other
    {
      return false;
    }
  }
}
//------------------------------------------------------------------------------------------------------
template <class DT>
bool operator>= (DocComponent<DT> &obj1, DocComponent<DT> &obj2)
{
  if (obj1.top >= obj2.top)
  {
    // obj1 is greater than or equal to obj2
    return true;
  }
  else
  {
    // obj1 has to be less than obj2
    return false;
  }
}
//------------------------------------------------------------------------------------------------------
template <class DT>
bool operator<= (DocComponent<DT> &obj1, DocComponent<DT> &obj2)
{
  if (obj1.top <= obj2.top)
  {
    // obj1 is less than or equal to obj2
    return true;
  }
  else
  {
    // obj1 has to greater than obj2
    return false;
  }
}
//------------------------------------------------------------------------------------------------------
template <class DT>
bool operator== (DocComponent<DT> &obj1, DocComponent<DT> &obj2)
{
  if (obj1.top == obj2.top) // first field 'top' is equal, so compares the next field 'left' 
  {
    if (obj1.left == obj2.left)
    {
      // 'left' fields are equal, the objects are equal
      return true;
    }
    else
    {
      // 'left' fields ARE NOT equal, the objects ARE NOT equal
      return false;
    }
  }
  return false; // first field 'top' is NOT equal, so they cannot be equal, false!
}
//------------------------------------------------------------------------------------------------------
template <class DT>
bool operator!= (DocComponent<DT> &obj1, DocComponent<DT> &obj2)
{
  // returns the opposite of the == operation
  return !(obj1 == obj2);
}

void main()
{
  DocComponent<float>* d = new DocComponent<float>(1.19, 2.78, 3.44);
  cout << *d << endl;
  delete d;	d = NULL;
} 
Topic archived. No new replies allowed.