T::iterator 错误,其中模板参数 T 可能是 vector或列表

发布于 2024-09-24 00:58:59 字数 1725 浏览 0 评论 0原文

我正在尝试编写一个函数来打印常见 STL 容器(向量、列表等)的表示。我给了该函数一个模板参数 T,例如,它可能代表向量。我在获取类型 T 的迭代器时遇到问题

vector<int> v(10, 0);
repr< vector<int> >(v);

template <typename T>
void repr(const T & v)
{
    cout << "[";
    if (!v.empty())
    {
        cout << ' ';
        T::iterator i;
        for (i = v.begin(); 
             i != v.end()-1;
             ++i)
        {
            cout << *i << ", ";
        }
        cout << *(++i) << ' ';
    }
    cout << "]\n";
}

......

brett@brett-laptop:~/Desktop/stl$ g++ -Wall main.cpp
main.cpp: In function ‘void repr(const T&)’:
main.cpp:13: error: expected ‘;’ before ‘i’
main.cpp:14: error: ‘i’ was not declared in this scope
main.cpp: In function ‘void repr(const T&) [with T = std::vector<int, std::allocator<int> >]’:
main.cpp:33:   instantiated from here
main.cpp:13: error: dependent-name ‘T::iterator’ is parsed as a non-type, but instantiation yields a type
main.cpp:13: note: say ‘typename T::iterator’ if a type is meant

按照编译器的建议尝试了“typename T::iterator”,但只得到了一个更神秘的错误。

编辑:谢谢大家的帮助!对于想要使用此功能的任何人来说,这是一个工作版本:

template <typename T>
void repr(const T & v)
{
    cout << "[";
    if (!v.empty())
    {
        cout << ' ';
        typename T::const_iterator i;
        for (i = v.begin(); 
             i != v.end();
             ++i)
        {
            if (i != v.begin())
            {
                cout << ", ";
            }
            cout << *i;
        }
        cout << ' ';
    }
    cout << "]\n";
}

I'm trying to write a function to print a representation of common STL containers (vector, list, etc..). I gave the function a template parameter T which, for example, might represent vector. I'm having problems getting an iterator of type T.

vector<int> v(10, 0);
repr< vector<int> >(v);

...

template <typename T>
void repr(const T & v)
{
    cout << "[";
    if (!v.empty())
    {
        cout << ' ';
        T::iterator i;
        for (i = v.begin(); 
             i != v.end()-1;
             ++i)
        {
            cout << *i << ", ";
        }
        cout << *(++i) << ' ';
    }
    cout << "]\n";
}

...

brett@brett-laptop:~/Desktop/stl$ g++ -Wall main.cpp
main.cpp: In function ‘void repr(const T&)’:
main.cpp:13: error: expected ‘;’ before ‘i’
main.cpp:14: error: ‘i’ was not declared in this scope
main.cpp: In function ‘void repr(const T&) [with T = std::vector<int, std::allocator<int> >]’:
main.cpp:33:   instantiated from here
main.cpp:13: error: dependent-name ‘T::iterator’ is parsed as a non-type, but instantiation yields a type
main.cpp:13: note: say ‘typename T::iterator’ if a type is meant

I tried 'typename T::iterator' as the compiler suggested, but only got a more cryptic error.

Edit: Thanks for the help guys! Here's a working version for anyone who wants to use this function:

template <typename T>
void repr(const T & v)
{
    cout << "[";
    if (!v.empty())
    {
        cout << ' ';
        typename T::const_iterator i;
        for (i = v.begin(); 
             i != v.end();
             ++i)
        {
            if (i != v.begin())
            {
                cout << ", ";
            }
            cout << *i;
        }
        cout << ' ';
    }
    cout << "]\n";
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

转角预定愛 2024-10-01 00:58:59

您需要 typename 告诉编译器 ::iterator 应该是一种类型。编译器不知道它是一个类型,因为在实例化模板之前它不知道 T 是什么。例如,它还可以引用某些静态数据成员。这是你的第一个错误。

您的第二个错误是 v 是对 const 的引用。因此,您必须使用 ::const_iterator 而不是 ::iterator。您不能向常量容器请求非常量迭代器。

You need typename to tell the compiler that ::iterator is supposed to be a type. The compiler doesn't know that it's a type because it doesn't know what T is until you instantiate the template. It could also refer to some static data member, for example. That's your first error.

Your second error is that v is a reference-to-const. So, instead of ::iterator you have to use ::const_iterator. You can't ask a constant container for a non-const iterator.

甚是思念 2024-10-01 00:58:59

T::iterator i; 更改为 typename T::const_iterator i; 因为 ::iterator 的类型为 Tv 是一个 const &

在限定依赖类型之前,您需要typename
如果没有 typename,则 C++ 解析规则规定,合格的从属名称应解析为 非类型,即使它会导致语法错误。

typename 声明后面的名称应被视为类型。否则,名称将被解释为引用非类型。

Change T::iterator i; to typename T::const_iterator i; because ::iterator is of type T and v is a const &.

Before a qualified dependent type, you need typename.
Without typename, there is a C++ parsing rule that says that qualified dependent names should be parsed as non-types even if it leads to a syntax error.

typename states that the name that follows should be treated as a type. Otherwise, names are interpreted to refer to non-types.

愁以何悠 2024-10-01 00:58:59

也许这会有所帮助:

Typename 在引用类型的限定依赖名称之前是强制性的,除非该名称命名基类或在初始化列表中。在模板中使用限定(但非依赖名称)之前,类型名称是可选的,但在命名基类或初始化列表时除外。

Maybe this will help:

Typename is mandatory before a qualified-dependent name which refers to a type, unless that name is naming a base class, or in an initialization list. Typename is optional before a qualified (but non-dependent name) is used within a template, except again when naming a base class or in an initialization list.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文