Templates 共享指针的向量迭代器

Templates 共享指针的向量迭代器,templates,c++11,vector,iterator,shared-ptr,Templates,C++11,Vector,Iterator,Shared Ptr,我正在尝试获取一个迭代器到我的共享向量\u ptr。它给了我两个编译错误: syntax error : missing ';' before identifier 'begin' missing type specifier - int assumed. Note: C++ does not support default-int 在代码中,如果我将向量的条目类型替换为字符串或基本类型,它将编译。[当然这不是我想要的]。发生什么事了 #include <vector> #incl

我正在尝试获取一个迭代器到我的共享向量\u ptr。它给了我两个编译错误:

syntax error : missing ';' before identifier 'begin'
missing type specifier - int assumed. Note: C++ does not support default-int
在代码中,如果我将向量的条目类型替换为字符串或基本类型,它将编译。[当然这不是我想要的]。发生什么事了

#include <vector>
#include <unordered_map>
#include <memory>

template<class _Kty, class _Ty> class MyClass {
public:
    typedef std::shared_ptr<std::pair<_Kty, _Ty>> Entry;

    // Vector containing pairs of the key & value
    std::vector<Entry> container;

    // Beginning iterator to the vector part
    std::vector<Entry>::iterator begin() noexcept {
        return containervector.begin();
    }
};
#包括
#包括
#包括
模板类MyClass{
公众:
typedef std::共享的ptr条目;
//包含键和值对的向量
载体容器;
//从迭代器开始到向量部分
std::vector::迭代器begin()noexcept{
返回containervector.begin();
}
};
std::vector::iterator是一个依赖名称

使用
typename
作为编译器关于嵌套的
::迭代器的提示:

typename std::vector::iterator begin()noexcept{
// ^^^^^^^^
问答部分:

为什么
typedef std::string Entry;
在没有
typename
的情况下工作

std::string
是模板
std::basic_string
的显式实例化,因此,编译器了解此类型及其嵌套成员类的所有信息

为什么我需要一个
typename
用于
shared\u ptr
版本的代码


这是因为
std::vector
后面隐藏的内容取决于提供给模板的参数。也就是说,
std::vector::迭代器
将根据
条目的内容而变化,而
条目本身利用模板参数(因为它具有
std::pair
),并且,
迭代器成员可能根本不存在,或者它可能是一个静态对象而不是一个类型。
typename
是对编译器的一个提示。

有效!但我还有一个问题。我将typedef行更改为:typedef std::string Entry;&it compiled。为什么我需要一个typename用于共享的代码版本?[&不是字符串版本]
   typename std::vector<Entry>::iterator begin() noexcept {
// ^^^^^^^^