'std::vector<T>::iterator it;'不编译 [英] &#39;std::vector&lt;T&gt;::iterator it;&#39; doesn&#39;t compile

查看:46
本文介绍了'std::vector<T>::iterator it;'不编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个功能:

    template<typename T>
    void Inventory::insertItem(std::vector<T>& v, const T& x)
    {
        std::vector<T>::iterator it; // doesn't compile
        for(it=v.begin(); it<v.end(); ++it)
        {
            if(x <= *it) // if the insertee is alphabetically less than this index
            {
                v.insert(it, x);
            }
        }
    }

和 g++ 给出这些错误:

and g++ gives these errors:

src/Item.hpp: In member function ‘void
yarl::item::Inventory::insertItem(std::vector<T, std::allocator<_CharT> >&, const T&)’:  
src/Item.hpp:186: error: expected ‘;’ before ‘it’  
src/Item.hpp:187: error: ‘it’ was not declared in this scope

一定是很简单的东西,但是看了十分钟后,我找不到任何问题.还有人看吗?

it must be something simple, but after ten minutes of staring at it I can't find anything wrong. Anyone else see it?

推荐答案

试试这个:

typename std::vector<T>::iterator it;

这是一个页面,描述了如何使用 typename 以及为什么它是必要的在这里.

Here's a page that describes how to use typename and why it's necessary here.

这篇关于'std::vector<T>::iterator it;'不编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆