错误 C2899:不能在模板声明外部使用类型名

error C2899: typename cannot be used outside a template declaration

本文关键字:外部 类型 声明 C2899 不能 错误      更新时间:2023-10-16

我正在尝试以下MSV2010

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:无法在模板声明外部使用类型名

解决方法方面的帮助将不胜感激。

namespace statismo {
template<>
struct RepresenterTraits<itk::Image<itk::Vector<float, 3u>, 3u> > {
    // bla
    typedef typename VectorImageType::PointType PointType;
            ^^^^^^^^
    typedef typename VectorImageType::PixelType ValueType;
            ^^^^^^^^
};

您的typename关键字放置在itk::Image<itk::Vector<float, 3u>, 3u>RepresenterTraits<T>的显式指定中。但是,这是一个常规类,而不是类模板。这意味着VectorImageType不是依赖名称,编译器知道PixelType是嵌套类型。这就是为什么不允许使用typename.请参阅此问答

请注意,在 C++11 中,此限制已取消,允许使用typename,但在非模板上下文中不是必需的。例如,请参阅此示例

#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()
{
}

实时示例(只需使用 g++/clang 和 std=c++98/std=c++11 命令行选项(。