【发布时间】:2015-04-03 17:13:57
【问题描述】:
假设我有一个基于模板ThingType 的课程。在标题中,我使用它来typedef 一个依赖类型VectorThingType。我想从方法GetVectorOfThings() 中返回它。如果我将VectorThingType 设置为返回类型,则会收到Does not name a type 错误,因为该类型未在此范围内定义。有没有什么方法可以在不复制typedef中的代码的情况下做到这一点?
#include <vector>
#include <iostream>
template< typename ThingType >
class Thing
{
public:
ThingType aThing;
typedef std::vector< ThingType > VectorThingType;
VectorThingType GetVectorOfThings();
Thing(){};
~Thing(){};
};
template< typename ThingType >
//VectorThingType // Does not name a type
std::vector< ThingType > // Duplication of code from typedef
Thing< ThingType >
::GetVectorOfThings() {
VectorThingType v;
v.push_back(this->aThing);
v.push_back(this->aThing);
return v;
}
【问题讨论】:
标签: c++ class templates typedef