Reputation: 338
I have problem with defining function max for template class. In this class we kept a numbers not as simple integers, but as vector of numbers in some numeral system. And with defining numeric_limits i need to return representation of maximal number founded on a defined number system.
And i get many errors, when i'm trying to return class with maximal representation, but it works when returns integer.
My template class:
template<int n,typename b_type=unsigned int,typename l_type=unsigned long long,long_type base=bases(DEC)>
class NSizeN
{
public:
int a_size = 0;
vector <b_type> place_number_vector; // number stored in the vector
NSizeN(int a){ //constructor
do {
place_number_vector.push_back(a % base);
a /= base;
a_size ++;
} while(a != 0);
}
void output(ostream& out, NSizeN& y)
{
for(int i=a_size - 1;i >= 0;i--)
{
cout << (l_type)place_number_vector[i] << ":";
}
}
friend ostream &operator<<(ostream& out, NSizeN& y)
{
y.output(out, y);
return out << endl;
}
}
At the end of .h file i have this :
namespace std{
template<int n,typename b_type,typename l_type,long_type base>
class numeric_limits < NSizeN< n, b_type, l_type, base> >{
public :
static NSizeN< n, b_type, l_type, base> max(){
NSizeN< n, b_type, l_type, base> c(base -1);
return c;
}
}
I've tried this with const, with constexpr, and it doesn't work. I've no idea how to get rid of this errors :
error: cannot bind 'std::ostream {aka std::basic_ostream<char>}' lvalue to'std::basic_ostream<char>&&'
std::cout << std::numeric_limits<NSizeN<3> >::max() << endl;
error: initializing argument 1 of 'std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = NSizeN<3>]'
operator<<(basic_ostream<_CharT, _Traits>&& __os, const _Tp& __x)
And this is what i'm trying do in main:
std::cout << std::numeric_limits<NSizeN<3> >::max() << endl;
This is my assignment so don't judge the way of doing this, because it's my teacher choice and I hope I presented my problem fairly comprehensive.
Upvotes: 2
Views: 762
Reputation: 3977
The problem that you are facing is that you try to bind a temporary returned by your max()
function to a non-const reference for the output operator.
The cleanest solution would be to declare the output operator as:
friend ostream &operator<<(ostream& out, const NSizeN& y)
and your output
function as
void output(ostream& out) const
Note, that I also removed the unused second parameter for the output
function. The const reference can be bound to both l-values and r-values, so it will work for the temporary returned by the max()
function as well.
Edit
As @n.m. pointed out, you also don't use the stream that is actually passed to the operator <<
and just use the std::cout
. The proper way to implement it is to simply use the stream (in your case just replace the cout << ...
with out << ...
in your output
function. This will let statements such as std::cerr << std::numeric_limits<NSizeN<3> >::max();
work as intended.
Upvotes: 2