Hi all.
I have run into a problem of overloading a templatized operator>> by a
specialized version of it. In short (complete code below), I have
written a stream class, STR, which defines a templatized operator>>() as
a member that can deal with the built in C types (int, char, float...)
template <class tType> STR& STR::operator>>(tType & t);
I then attempted to add an overloaded version of this to support my own
class, Array3d (a template class), and wrote this as a friend of Array3d as
template <class tType>
STR & operator >> ( STR & outf, Array3d<tType> & array ){}
It all compiles, but when I run the program, the lines;
str << array // str is an STR, array is an Array3d<int>
it will call STR::operator>> <Array3d<int> >, rather than (what I
belived to be) the specialized operator>>(STR &, Array3d<int> &).
If I instead remove the STR::operator>>() and replace it by a friend
function (commented out in the code below) the function call works as i
expect it to.
Can you please enlighten me as to where I have gone wrong?
Best regards
hall
// main.cpp
//---------------------------------------------------------------------------
#pragma hdrstop
#include "IOFile_mod.h"
#include <iostream>
#pragma argsused
int main(int argc, char* argv[])
{
Array3d<int> array;
std::cout << "Operator << for Array3d\n";
STR in;
in >> array;
std::cout << "Operator << for int\n";
int i(3);
in >> i;
return 0;
}
//---------------------------------------------------------------------------
// IOFile_mod.h
//---------------------------------------------------------------------------
#ifndef IOFileH
#define IOFileH
#include <iostream>
// --------------------------------------
class STR
{
public:
template <class tType> STR& operator>>( tType & );
//template <class tType> friend STR & operator >> ( STR & of, tType
& a );
};
template <class tType>
STR& STR::operator>>( tType & obj)
{
std::cout << "STR::operator<<(const tType obj)\n";
return *this;
}
//template <class tType>
//STR & operator >> ( STR & of, tType & a ){
// std::cout << "Friend of STR\n";
// return of;
//}
// --------------------------------------
template <class tType> class Array3d;
template <class tType> STR & operator >> ( STR & of, Array3d<tType> & a );
template <class tType> class Array3d
{
public:
friend STR & operator >> <>( STR & of, Array3d<tType> & a );
};
template <class tType>
STR & operator >> ( STR & outf, Array3d<tType> & array )
{
std::cout << "STR & operator >> (STR & inf, Array3d<tType> & array )\n";
return outf;
}
#endif
//---------------------------------------------------------------------------
--
Please remove capital x from email to reply