如何保持迭代器类型的通用性?

时间:2017-12-22 17:16:54

标签: c++ templates generic-programming

我在使用模板类的实例变量时遇到了一些麻烦。我的目标是存储一个整数和迭代器对的列表。迭代器来自B类对象列表。我的问题是编译器要我声明类B的类型,但是对于我的应用程序,我想在此时保持它的通用性。这可能吗?

template <typename T>
class A {
  public:
    A() {}
    std::pair<int, std::list<B<T>>::iterator> foo; /* Does not compile. See error 1. */
    std::list<B<T>>::iterator bar; /* Does not compile. See error 2. */

    std::pair<int, std::list<B<int>>::iterator> foo; /* Compiles just fine. */
    std::list<B<int>>::iterator bar; /* Compiles just fine. */

错误1:'template struct std :: pair'的模板参数列表中参数2的类型/值不匹配              的std ::列表&gt; ::迭代&GT;

注意:期待一个类型,得到'std :: __ cxx11 :: list&gt; :: iterator'

错误2:在'std :: __ cxx11 :: list&gt; :: iterator'之前需要'typename'因为'std :: __ cxx11 :: list&gt;'是一个依赖范围    std :: list&gt; :: iterator bar;

0 个答案:

没有答案