2011-06-27 87 views
11

我有這樣的代碼:嵌套名稱 - 符

namespace mymap { 
    template <class Key,template <typename T > class Allocator> myownmap { 
     typedef pair<const unsigned int, Key> typename _myPair; 
     typedef multimap<unsigned int, Key,less<Key> ,Allocator<_myPair> > typename _entriesType; 
    } 
} 

它編譯成功(和作品)MSVC下,但是GCC抱怨無效的語法:

.hpp:20: error: expected nested-name-specifier before ‘_myPair’ 
.hpp:20: error: two or more data types in declaration of ‘_myPair’ 

我是什麼做錯了?

+2

你真的用gcc測試了這個代碼嗎?您在myownmap之前缺少類/結構,並且「entriesPair」不會在代碼段中的任何位置出現。 – Philipp

+0

你能告訴我們'entriesPair'的定義嗎? –

+0

什麼是myownmap?它是一個功能還是類? – iammilind

回答

16

typename在那裏不需要,因此是不允許的。

MSVC不正確地解析模板直到它們被實際使用,所以直到後來才發現一些錯誤。

3

「預期的嵌套名稱說明符」意味着在typename關鍵字之後,預計將使用模板參數的某個嵌套名稱,例如typedef typename Key::iterator ...。在你的情況下,你不必使用typename

4
typedef pair<const unsigned int, Key> /*typename*/ _myPair; 
             ^^^^^^^^^^^^ not needed 

查看gcc-4.5 output here。 (它適用於myownmapclass或功能)