2013-10-07 10 views
4

我想在MSV2010錯誤C2899:類型名稱不能模板聲明之外使用

namespace statismo { 
template<> 
struct RepresenterTraits<itk::Image<itk::Vector<float, 3u>, 3u> > { 

    typedef itk::Image<itk::Vector<float, 3u>, 3u> VectorImageType; 
    typedef VectorImageType::Pointer DatasetPointerType; 
    typedef VectorImageType::Pointer DatasetConstPointerType; 
    typedef typename VectorImageType::PointType PointType; 
    typedef typename VectorImageType::PixelType ValueType; 
}; 

下面我收到以下錯誤:

錯誤C2899:類型名稱不能被模板聲明外使用

在解決方法的幫助將不勝感激。

回答

9
namespace statismo { 
template<> 
struct RepresenterTraits<itk::Image<itk::Vector<float, 3u>, 3u> > { 

    // bla 

    typedef typename VectorImageType::PointType PointType; 
      ^^^^^^^^ 

    typedef typename VectorImageType::PixelType ValueType; 
      ^^^^^^^^ 
}; 

typename關鍵字放置RepresenterTraits<T>明確specalization內爲itk::Image<itk::Vector<float, 3u>, 3u>。但是,這是一個普通的類,而不是類模板。這意味着VectorImageType不是從屬名稱並且編譯器知道PixelType是嵌套類型。這就是爲什麼不允許使用typename。參見als this Q&A

請注意,在C++ 11中,此限制已取消,並且在非模板上下文中允許使用,但不要求使用。見例如這個例子中

#include <iostream> 

template<class T> 
struct V 
{ 
    typedef T type; 
}; 

template<class T> 
struct S 
{ 
    // typename required in C++98/C++11 
    typedef typename V<T>::type type;  
}; 

template<> 
struct S<int> 
{ 
    // typename not allowed in C++98, allowed in C++11 
    // accepted by g++/Clang in C++98 mode as well (not by MSVC2010) 
    typedef typename V<int>::type type;  
}; 

struct R 
{ 
    // typename not allowed in C++98, allowed in C++11 
    // accepted by g++ in C++98 mode as well (not by Clang/MSVC2010) 
    typedef typename V<int>::type type;  
}; 

int main() 
{ 
} 

Live Example(只是與G ++ /鐺和std = C++ 98/STD = C++ 11的命令行選項播放)。

+0

非常感謝。這很清楚,儘管它不是我的代碼,我也不清楚我可以做什麼作爲快速解決方法 –

+0

@TinasheMutsvangwa很高興能夠提供幫助! – TemplateRex

+0

@TinasheMutsvangwa快速解決方法是在這兩個最後的typedef中刪除typename。雖然它不是你的代碼,但有什麼能夠阻止你編輯它嗎? –

相關問題