2015-02-06 149 views
-2

我有一個模板類中定義的typedef:呼叫類型定義的構造函數中的模板

template <class T> 
class ROIAlg{ 
    public: 
    typedef std::vector<T> Waveform; 
    typedef typename Waveform::const_iterator Tick; 
    typedef std::pair<Tick, Tick> Region; 
    //etc. 
}; 

,並使用它們

template <class T> 
class WaveformPropertiesAlg{ 
    public: 
    WaveformPropertiesAlg(); 
    private: 
    typename ROIAlg<T>::Region fCPR; 
    //etc. 
}; 

WaveformPropertiesAlg的構造函數的實現其他類:

template <class T> 
WaveformPropertiesAlg<T>::WaveformPropertiesAlg(): 
    fCPR(ROIAlg<T>::Tick(), ROIAlg<T>::Tick()) { 
    //etc 
} 

另一段代碼試圖構造一個WaveformPropertiesAlg<short int>但comp平息失敗:

In instantiation of ‘WaveformPropertiesAlg<T>::WaveformPropertiesAlg() [with T = short int]’: 
RawDigitAndWireComparisonAlg.cxx:13:77: required from here 
WaveformPropertiesAlg.h:30:51: error: dependent-name ‘ROIAlg<T>::Tick’ is parsed as a non-type, but instantiation yields a type 
    fCPR(ROIAlg<T>::Tick(),ROIAlg<T>::Tick()) 
        ^
WaveformPropertiesAlg.h:30:51: note: say ‘typename ROIAlg<T>::Tick’ if a type is meant 

我不認爲這裏是一個類型,因爲我調用了Tick的構造函數。我怎樣才能做到這一點?

+0

需要告訴編譯器'ROIAlg :: Tick'是一個typename。 – 2015-02-06 23:26:53

+1

我們可以每隔多久進行一次這樣的活動?爲什麼沒有人閱讀他們的編譯器的錯誤信息,但只是丟棄它們? – Walter 2015-02-06 23:31:31

回答

1

typedef typename Waveform::const_iterator Tick; < <這是一個類型,因此你需要typename,因爲編譯器會告訴你:

fCPR(typename ROIAlg<T>::Tick(), typename ROIAlg<T>::Tick()) 

中,你不需要typename是,如果Tick是別的東西,比如函數的情況下, :

template <class T> 
class ROIAlg{ 
    public: 
    static T Tick(); 
};