比方说,我有一个基于模板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;
}
真的很酷。谢谢! – 2015-04-03 17:30:05