簡體   English   中英

C ++:specializing成員需要«template <>»語法

[英]C++: specializing member requires «template<>» syntax

我究竟做錯了什么?

template<class T>
class Binder
{
public:
    static std::vector< Binder< T >* > all;
    Node<T>* from;
    Node<T>* to;
    Binder(Node<T>* fnode, Node<T>* tonode)
    {
        from = fnode;
        to = tonode;
        Binder<T>::all.push_back(this);
    }
};

std::vector<Binder<int>*> Binder<int>::all = std::vector< Binder<int>* >(); //here it is

謝謝。

編譯器將靜態成員的定義解釋為專門化(實際上,它一種特殊化:您提供特定於T = int的聲明)。 這可以通過在定義之前添加template<>來修復。

在模板中定義靜態成員有點令人失望:靜態成員需要在標題之外定義,只有當您已經知道綁定器的所有可能的T時才可能。

例如,現在您正在為T=int定義它。 現在,如果你開始在某處使用Binder<double> ,靜態成員將是一個未定義的引用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM