2010-11-15 70 views
1

我寫了一個小級,以協助轉換和從MSVC的笨重類型:這個簡單的模板類有什麼問題嗎?

template <class FromType> 
struct convert 
{ 
    convert(FromType const &from) 
     : from_(from) {} 
    operator LARGE_INTEGER() { 
     LARGE_INTEGER li; 
     li.QuadPart = from_; 
     return li; 
    } 
private: 
    FromType const &from_; 
}; 

後來我這樣做:

convert(0) 

,並從MSVC此錯誤消息:

1> e:\ src \ cpfs \ libcpfs \ device.cc(41):error C2955:'convert':使用類模板需要模板參數列表

1> E:\ SRC \ CPFS \ libcpfs \ device.cc(17):看 '轉換'

我認爲FromType可以從我傳遞的整數推斷的聲明?到底是怎麼回事?

+0

U需要做這樣的轉換(0) – vinothkr 2010-11-15 05:30:17

回答

4

類模板從未隱式地實例化。鑑於你給的類定義,你不得不說:

convert<int>(0) 

...來調用類的構造函數。

在默認模板參數,你可以提高它(?):

template <class FromType = int> 
struct convert 
{ /* ... */ }; 

,然後調用它:

convert<>(0) 

...但我恐怕這是最好的你可以用類模板做。你可能反而想用一個函數模板實例化類對象爲您提供:

template <typename FromType> 
convert<FromType> make_convert(FromType from) { 
    return convert<FromType>(from); 
} 

性病這是或多或少的方式使用:: make_pair()爲例。

+0

謝謝。我去了一個轉換工廠和轉換器班。後來我把它們扔出去做手動,但是我:) – 2010-11-15 09:45:04

相關問題