operator<< with template class doesn't work
Hi,
I am trying to output a template class to 'cout' but my code doesn't
compile; here is gcc's error message:
/tmp/ccnNaceV.o: In function `main':
main.cpp:(.text+0x6d): undefined reference to
`std::basic_ostream<char, std::char_traits<char> >& operator<<
<char>(std::basic_ostream<char, std::char_traits<char> >&,
Sudoku<char>&)'
collect2: ld returned 1 exit status
and here is the code:
157 template<class T>
158 ostream& operator<<(ostream& os, const Sudoku<T>& s)
159 {
160 for (char i=0; i < s.size; i++)
161 {
162 for (char j=0; j < s.size; j++)
163 {
164 if ( s.is_true(i, j) && HAS_BOLD )
165 {
166 os << "\033[1m" << s.get(i,j) << "\033[0m\t";
167 }
168 else
169 os << s.matrix->at(i,j) << "\t";
170 }
171 os << "\n";
172 }
173 return os;
174 }
175
176 int main()
177 {
178 Sudoku<char> s;
179 //cout << s;
180 operator<<(cout, s);
181 return 0;
182 }
I have declared
friend ostream& operator<<(ostream& os, Sudoku<T2>& s);
in the template class Sudoku.
Thanks.