【发布时间】:2016-08-05 10:08:27
【问题描述】:
我正在编写一个模板类,它的行为应该像一个容器。内部数据是指向泛型类T 的智能指针向量。
标题
#include <vector>
#include <boost/shared_ptr.hpp>
namespace Foo
{
template <class T>
class AstrContainer
{
public:
typedef boost::shared_ptr<T> p_element;
typedef std::vector<p_element> v_p_elements;
protected:
v_p_elements _vData;
public:
AstrContainer();
virtual ~AstrContainer();
typename v_p_elements::iterator begin();
typename v_p_elements::iterator end();
};
}
来源
#include "AstrContainer.hpp"
namespace Foo
{
template<class T>
AstrContainer<T>::AstrContainer()
{
}
template<class T>
AstrContainer<T>::~AstrContainer()
{
}
typename
v_p_elements::iterator AstrContainer<T>::begin() // - - - ERROR LINE 1 - - -
{
return _vData.begin();
}
template<class T>
typename v_p_elements::iterator AstrContainer<T>::end() // - - - ERROR LINE 2 - - -
{
return _vData.end();
}
}
我对 C++ 中的模板类非常陌生,有点卡在 ERROR LINE 1
错误 C2653:“v_p_elements”:不是类或命名空间名称
所以我评论了 begin() 方法,但在 ERROR LINE 2 它停止并出现相同的错误。
现在似乎很清楚,因为v_p_elements 是在类内部进行类型定义的,所以它可能无法导出到外部世界。但现在我要问的是整个事情是否可能,或者我只是误解了什么。
【问题讨论】:
标签: c++ templates typedef template-classes