我正在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:无法在模板声明之外使用typename
在解决办法方面的帮助将是非常感谢的。
发布于 2013-10-07 13:13:51
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的使用是允许的,但不是必需的。参见这个例子。
#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命令行选项)。
https://stackoverflow.com/questions/19225458
复制相似问题