It seems like I was unable to declare and implement the insertion (<<
) and extraction (>>
) operators for a class properly (note that both of them worked fine before I made the class a class template by adding a type template parameter to it).
I have this class template:
template < class Allocator = std::allocator<char> >
class CharMatrix
{
// ...
};
Inside the class in the header file:
template <class Allocator>
friend std::ofstream& operator<<( std::ofstream& ofs, const CharMatrix<Allocator>& char_matrix );
template <class Allocator>
friend std::ifstream& operator>>( std::ifstream& ifs, CharMatrix<Allocator>& char_matrix );
And in the source file:
template <class Allocator>
std::ofstream& operator<<( std::ofstream& ofs, const CharMatrix<Allocator>& char_matrix )
{
// ...
}
template <class Allocator>
std::ifstream& operator>>( std::ifstream& ifs, CharMatrix<Allocator>& char_matrix )
{
// ...
}
I'm getting this error:
CharMatrix.h:99:19: error: declaration of template parameter ‘Allocator’ shadows template parameter
99 | template <class Allocator>
| ^~~~~
CharMatrix.h:35:12: note: template parameter ‘Allocator’ declared here
35 | template < class Allocator = std::allocator<char> >
| ^~~~~
What is the proper syntax for this?