I am trying to play around with templates and I am coming across a strange error and it is strictly related to using the ostream function
function in header file:
#include <iostream>
using namespace std;
friend ostream& operator <<(ostream& outs, const GenericList<ItemType>& the_list);
// Overlaods the << operator so it can be used to output the contents of the list.
// The items are output one per line.
// Precondition: If outs is a file output stream, then outs has already been connected to a file
function definition in cpp file:
#include <iostream>
#include <cstdlib>
#include "genericlist.h"
template<class ItemType>
ostream& operator <<(ostream& outs, const GenericList<ItemType>& the_list)
{
for(int i = 0; i < the_list.current_length; i++)
outs << the_list.item << endl;
return outs;
}
This is how I am calling it in the driver file:
#include <iostream>
#include "genericlist.h"
#include "genericlist.cpp"
using namespace std;
GenericList<int> first_list(2);
first_list.add(1);
first_list.add(2);
cout << "first_list = \n" << first_list;
It is giving me a link error and I am absolutely confused as to why:
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 GenericList<int> const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABV?$GenericList@H@@@Z) referenced in function _main
1>C:\Documents and Settings\gdesigner\Desktop\Problem Solving with C++ 5th Edition\Chapter 14\Project 2\Debug\Project 2.exe : fatal error LNK1120: 1 unresolved externals
1>Build log was saved at "file://c:\Documents and Settings\gdesigner\Desktop\Problem Solving with C++ 5th Edition\Chapter 14\Project 2\Project 2\Debug\BuildLog.htm"