C++ 为模板类定义数值_限制max函数

C++ 为模板类定义数值_限制max函数,c++,template-classes,numeric-limits,C++,Template Classes,Numeric Limits,我在为模板类定义函数max时遇到问题。在这门课上,我们把一个数字不是简单的整数,而是一些数字系统中的数字向量。通过定义数值_极限,我需要返回建立在定义的数字系统上的最大数的表示 当我试图返回具有最大表示的类时,我会遇到很多错误,但当返回整数时,它会起作用 我的模板类: template<int n,typename b_type=unsigned int,typename l_type=unsigned long long,long_type base=bases(DEC)> cla

我在为模板类定义函数max时遇到问题。在这门课上,我们把一个数字不是简单的整数,而是一些数字系统中的数字向量。通过定义数值_极限,我需要返回建立在定义的数字系统上的最大数的表示

当我试图返回具有最大表示的类时,我会遇到很多错误,但当返回整数时,它会起作用

我的模板类:

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;
    }
}
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;
     }
}
模板
类NSizeN
{
公众:
int a_size=0;
vector place_number_vector;//存储在向量中的数字
NSizeN(inta){//constructor
做{
放置数字向量。向后推(基数百分比);
a/=基础;
a_size++;
}而(a!=0);
}
无效输出(ostream&out、NSizeN&y)
{
对于(int i=a_size-1;i>=0;i--)
{

cout您面临的问题是,您试图将
max()
函数返回的临时值绑定到输出运算符的非常量引用

最干净的解决方案是将输出运算符声明为:

friend ostream &operator<<(ostream& out, const NSizeN& y)
注意,我还删除了
output
函数未使用的第二个参数。const引用可以绑定到l值和r值,因此它也适用于
max()
函数返回的临时值

编辑
正如@n.m.所指出的,如果仔细观察,您也不会使用实际传递给
操作符的流,如果未使用,
输出的第一个参数。
    std::cout << std::numeric_limits<NSizeN<3> >::max() << endl;
friend ostream &operator<<(ostream& out, const NSizeN& y)
void output(ostream& out) const