错误 C2899:不能在模板声明外部使用类型名
error C2899: typename cannot be used outside a template declaration
我正在尝试以下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 命令行选项(。
相关文章:
- 调用外部函数,无法指定类型 C++/MVS
- 使用外部定义的模板类型作为模板参数的更通用模板的模板别名
- 外部基础类型未按请求对齐时的atomic_ref
- Pybind11:作为返回值的外部类型
- C++:在"try"外部创建的类型会导致错误,但在内部不会
- 外部 "C" Visual Studio 2015 中的显式类型错误,DLL 测试代码
- 函数类型定义可以与外部"C"相关联吗?
- 从内部类型参数推断外部类型
- 使用从外部参数包中获取的参数类型声明函数
- 为什么对非类型模板参数的引用需要外部链接
- 获取外部媒体类型
- 外部函数指针声明和类型推断定义
- C++:检查是否在没有外部库的情况下抛出了某个异常类型
- 外部模板,用于使用非竞争类型参数化的模板
- 可变类型的部分模板专门化和扩展到外部类型的可变参数包会导致歧义
- C 中的外部类型声明冲突
- SWIG:使用导入的外部类型
- 无法读取外部类型和资源系统的文件
- 如何在C++中推导内部类型的外部类型
- 当使用许多外部类型声明时,如何加快编译时间?