error C2899: typename cannot be used outside a template declaration

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;//出现错误

};

C++出现error C2899原因:

VectorImageType是已经实例化的模板类,编译器已经知道PointType是VetorImageType的类型名,而不是其静态变量,即PointType是嵌套类型。故不需要typename。

在C++11,已经允许这种写法,但是不建议使用。

在下列情况下必须对一个name使用typename关键字:
1. 一个唯一的name(可以作为类型理解),它嵌套在另一个类型中的。
2. 依赖于一个模板参数。

例:

#include <iostream>
#include <typeinfo> // for typeid() operator
using namespace std;
template <typename TP>
struct COne {   // default member is public
    typedef TP one_value_type;
};
template <typename COne>   // 用一个模板类作为模板参数, 这是很常见的
struct CTwo {
    // 请注意以下两行
    // typedef COne:one_value_type two_value_type;   // 错误
    typedef typename COne:one_value_type two_value_type; // *正确
};
// 以上两个模板类只是定义了两个内部的public类型, 但请注意第二个类CTwo的two_value_type类型
// 依赖COne的one_value_type, 而后者又取决于COne模板类实例化时传入的参数类型.

参考:

【1】http://stackoverflow.com/questions/19225458/error-c2899-typename-cannot-be-used-outside-a-template-declaration

【2】http://blog.sina.com.cn/s/blog_67bccccb010157lp.html


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值