I have a template class with a friend operator, but when I try to use it I see undefined reference to `operator<<(std::ostream&, Numbers<10ul> const&)'
template <size_t Base>
class Numbers{
int digits;
public:
Numbers(int);
friend std::ostream& operator<<(std::ostream&, const Numbers<Base>&);
};
template <size_t Base>
Numbers<Base>::Numbers(int n){
digits = n;
}
template <size_t Base>
std::ostream &operator<<(std::ostream &out,const Numbers<Base> &a){
out << a.digits;
return out;
}
int main()
{
Numbers<10> example(1234);
std::cout<<example;
return 0;
}
I saw here that template classes don't work well if you declare it in the .h and you define it in the .cc, so I put it in the same file but it still doesn't find it.
I also saw a post saying to declare the functions before defining the class, as in
template <size_t Base> class Numbers;
template <size_t Base>
std::ostream &operator<<(std::ostream &out,const Numbers<Base> &a);
template <size_t Base>
class Numbers{
Rest of the file
But it also didn't work.