2011-10-17 26 views
1

詮釋這一類,其中運營商< <定義(見代碼),而試圖用gcc 4.6.1我得到以下錯誤編譯:在沒有比賽的「運營商< <」 'std :: cout < < a'。這是怎麼回事?缺少操作員<<但它的存在

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樣子:

#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; 
} 
+0

可以爲用戶提供一個例子主要? –

+0

@VJo編輯,見OP – smallB

+0

這是一些快樂的模板魔法。你在包裝標準的數字類型,不是嗎?如果不是祕密,你的目標是什麼?你爲什麼做這個? – Septagram

回答

2

你的模板有三個參數,類型,和已知最適合類型的兩個常數,但您的模板operator<<需要三個類型的模板實例化。

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訪問--unless還決定外聲明它)

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(); 
    } 
}; 
+0

那麼你會提出什麼建議? – smallB

+0

那麼你說的是我應該聲明它是一個類的成員?但是有沒有辦法將其聲明爲「獨立」的fnc? – smallB

+0

我編輯了答案,你可以做的最簡單的事情是在類作用域內定義運算符。或者你可以嘗試修復模板。沒有試過,所以沒有保證:'template :: type Min_Range,typename Best_Fit :: type Max_Range> std :: ostream&operator <<(std :: ostream&o,Int const&obj){...' –