2010-02-21 68 views
2

我有一個模板很奇怪的問題。獲取錯誤error: ‘traits’ is not a template。我無法在示例測試項目上重現此問題。但它發生在我的項目上(這比我可以在這裏發佈的要大)。錯誤:'性狀'不是模板 - C++

無論如何,以下是我的文件和用法。任何人有任何關於何時發生此錯誤的想法?

我在traits.hpp以下。

namespace silc 
{ 
    template<class U> 
    struct traits<U> 
    { 
     typedef const U& const_reference; 
    }; 

    template<class U> 
    struct traits<U*> 
    { 
     typedef const U* const_reference; 
    }; 
} 

這用於另一個頭文件。

namespace silc { 

    template<typename T> 
    class node {     
    public: 

     typedef typename traits<T>::const_reference const_reference; 

     const_reference value() const { 
      /* ... */ 
     } 
    } 
} 

回答

3

模板專門化的語法是...不愉快。

我相信你的錯誤可以通過將struct traits<U>替換爲struct traits(但是請保留原來的struct traits<U*>!)來解決。

但看看光明的一面!至少你不是在做功能類型的部分專業化:

// Partial class specialization for 
// function pointers of one parameter and any return type 
template <typename T, typename RetVal> 
class del_ptr<T, RetVal (*)(T*)> { ... }; 

// Partial class specialization for 
// functions of one parameter and any return type 
template <typename T, typename RetVal> 
class del_ptr<T, RetVal(T*)> { ... }; 

// Partial class specialization for 
// references to functions of one parameter and any return type 
template <typename T, typename RetVal> 
class del_ptr<T, RetVal(&)(T*)> { ... }; 
+1

謝謝。那是我犯過的一個愚蠢的錯誤。再次感謝您指出。 – 2010-02-21 06:12:11