首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >错误C2899:无法在模板声明之外使用typename

错误C2899:无法在模板声明之外使用typename
EN

Stack Overflow用户
提问于 2013-10-07 12:53:07
回答 1查看 7.6K关注 0票数 4

我正在MSV2010中尝试以下几种方法

代码语言:javascript
复制
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:无法在模板声明之外使用typename

在解决办法方面的帮助将是非常感谢的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-07 13:13:51

代码语言:javascript
复制
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>用于itk::Image<itk::Vector<float, 3u>, 3u>的显式规范中。但是,这是一个常规类,而不是类模板。这意味着VectorImageType不是依赖于的名称,编译器知道PixelType是嵌套类型。这就是不允许使用typename的原因。见als this Q&A

注意,在C++11中,这个限制已经取消,在非模板上下文中,typename的使用是允许的,但不是必需的。参见这个例子。

代码语言:javascript
复制
#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++/clang和std=c++98 / std=c++11命令行选项)。

票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19225458

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档