我必須聲明Iter
爲typename
,但不是typedef
。
然後,我不能在我的template
class
聲明中使用Iter
=>沮喪:(
請告訴我如何使用Iter
,否則解釋我爲什麼C++是如此nasty ...模板類嵌套類型typename
template <typename T>
struct MyContainer
{
typedef std::vector<T> Vec;
typename Vec::iterator Iter;
void Fo (typename std::vector<T>::iterator); //ok
typename std::vector<T>::iterator Ba(); //ok
void Foo (Iter); //error: 'Iter' is not a type
Iter Bar(); //error: 'Iter' does not name a type
}; //(gcc 4.1.2)
- 爲什麼
typedef
不能用來聲明Iter
?爲什麼不呢? - 如何內
template
class
使用Iter
?(例如,作爲函數參數類型)
EDIT
在指令typename Vec::iterator Iter;
關鍵字typename
說Vec::iterator
是一種類型(即不是靜態成員函數/屬性)。因此,Iter
未聲明爲類型,而是作爲使用Vec::iterator
類型的變量聲明。
完美:)我剛纔在你的答案代碼源解決方案:-D感謝加;) – olibre
@olibre關於您的編輯這個答案(我回滾了):可以在枚舉中通過縮進兩次使用blockquote(4 + 4 = 8個空格)。不需要任何不尋常的枚舉語法... – jogojapan
@jogojapan謝謝您的提示/建議:-D – olibre