缺少运算符<<但它在那里 [英] Missing operator<< but it's there

查看:295
本文介绍了缺少运算符<<但它在那里的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Int这个类,其中operator<<在尝试使用gcc 4.6.1编译它时定义(见代码)我得到以下错误:没有匹配'操作符<<'in'std :: cout< a'。这是怎么回事?

Int this class where operator<< is defined (see code) while trying to compile it with gcc 4.6.1 I'm getting following error: no match for 'operator<<' in 'std::cout << a'. What's going on?

template<class Int_T = int, typename Best_Fit<Int_T>::type Min_Range = std::numeric_limits<Int_T>::min(),
                            typename Best_Fit<Int_T>::type Max_Range = std::numeric_limits<Int_T>::max()>
class Int
{
Int_T data_;  

Int_T get_data()const
{
return data_;
}

};  
//Here is this operator defined
template<class Int_T>
std::ostream& operator<<(std::ostream& out, const Int<Int_T, Best_Fit<Int_T>::type, Best_Fit<Int_T>::type>& obj)
{
    out << obj.get_data();
    return out;
}

其中Best_Fit如下所示:

where Best_Fit looks like:

#ifndef BEST_FIT_H_INCLUDED
#define BEST_FIT_H_INCLUDED


struct Signed_Type
{
    typedef long long type;
};

struct Unsigned_Type
{
    typedef unsigned long long type;
};

template<bool Cond, class First, class Second>
struct if_
{
    typedef typename First::type type;
};

template<class First, class Second>
struct if_<false,First,Second>
{
    typedef typename Second::type type;
};

template<class Int_T>
struct Best_Fit
{//evaluate it lazily ;)
    typedef typename if_<std::is_signed<Int_T>::value,Signed_Type,Unsigned_Type>::type type;
};

#endif // BEST_FIT_H_INCLUDED

编辑:

#include <iostream>  
int main(int argc, char* argv[])
{
    Int<signed char,1,20> a(30);

    cout << a;
}


推荐答案

一个类型和两个常见的已知最佳拟合类型,但您的模板运算符< 三种类型。

Your template has three arguments, a type, and two constants of a known best fit type, but your templated operator<< takes an instantiation of the template with three types.

template<class Int_T = int, typename Best_Fit<Int_T>::type Min_Range
                                     = std::numeric_limits<Int_T>::min(), // constant!
                            typename Best_Fit<Int_T>::type Max_Range
                                     = std::numeric_limits<Int_T>::max()  // constant!
        >
class Int
//...
template<class Int_T>
std::ostream& operator<<(std::ostream& out, 
                         const Int<Int_T, 
                                   Best_Fit<Int_T>::type, // type!
                                   Best_Fit<Int_T>::type  // type!
                         >& obj)



我通常建议类模板在类定义中定义(使用 friend 在该上下文中定义一个自由函数)因为这个特殊的原因,在类模板中获取类型是很容易的,容易在其外失效。还有一些其他的差异(像事实,如果运算符被定义在类中,那么它只能通过ADL访问 - 无论你也决定在外部声明)

I usually recommend that operator overloads of class templates are defined inside the class definition (use friend to define a free function in that context) for this particular reason, it is trivial to get the types right inside the class template, and easy to fail outside of it. There are a couple other differences (like the fact that if the operator is defined inside the class then it will only be accessible through ADL --unless you also decide to declare it outside)

template<class Int_T = int, typename Best_Fit<Int_T>::type Min_Range
                                     = std::numeric_limits<Int_T>::min(), // constant!
                            typename Best_Fit<Int_T>::type Max_Range
                                     = std::numeric_limits<Int_T>::max()  // constant!
        >
class Int {
   friend                  // allows you to define a free function inside the class
   std::ostream& operator<<( std::ostream& out, 
                             Int const & obj ) {  // Can use plain Int to refer to this 
                                                  // intantiation. No need to redeclare
                                                  // all template arguments
       return out << obj.get_data();
   }
};

这篇关于缺少运算符&lt;&lt;但它在那里的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆