I've been searching for a while and the closest thing to an answer was over there
However I was not able to make it work in my class.
I have a Table2D.h
which contains this:
std::string toString() const;
std::ostream & operator<<( std::ostream & o, const Table2D<T> & s );
and I have a template class Table2D.template
which contains this:
template <class T>
std::ostream & :: operator<<( std::ostream & o, const Table2D<T> & s ){
return out << s.toString();
}
when I call my toString() function from main, it functions correctly.
However when I invoke the <<
operator using a std::cout
I get the following errors.
Table2D.h(59): error C2804: binary 'operator <<' has too many parameters
Table2D.h(85) : see reference to class template instantiation 'Table2D<T>' being compiled
Table2D.template(100): error C2039: '<<' : is not a member of '`global namespace''
Table2D.h(59): error C2804: binary 'operator <<' has too many parameters
just so you know the 59'th line contains
for (unsigned y=0; y<m_height; y++) col_ptr[y] = (T) col_ptr_src[y];
which as you see contains no <<
's so I'm not entirely certain what it's referring to.
Edit:
After removing the declaration from the class, I replaced it's entry in the header file with this
template <class T>
std::ostream& operator<<( std::ostream& o, const Table2D<T>& s ) {
return o << s.toString();
}
and got the following error:
Table2D.h(60): error C2804: binary 'operator <<' has too many parameters
Table2D.h(89) : see reference to class template instantiation 'Table2D<T>' being compiled
the 89th line in the template file contains std::stringstream resultStream;
which is the very first line in my toString function, which looks like this
template <class T>
std::string Table2D<T> :: toString() const{
std::stringstream resultStream;
for(unsigned i = 0; i< m_height; i++){
for (unsigned j = 0; j < m_width; j++){
resultStream << (*this)[i][j] << "\t";
}
resultStream << endl;
}
return resultStream.str();
}