c++ - 'std::vector<T>::iterator it ;' doesn' t 编译

标签 c++ stdvector

我有这个功能:

    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++ 给出了这些错误:

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

应该是很简单的东西,但是看了十分钟也没发现有什么不对。还有人看吗?

最佳答案

试试这个:

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

这是一个描述 how to use typename 的页面以及为什么这里有必要。

关于c++ - 'std::vector<T>::iterator it ;' doesn' t 编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3144604/

相关文章:

c++ - 更新存储在 map 中的 vector vector

c++ - 什么是 std::vector<std::string> vec{3};实际上呢?

c++ - 使用 X、Y、Z 索引到 std::vector

c++ - 使用iterator和reverse_iterator打印 vector 元素有没有更好的方法?

c++ - 动态多态和动态内存分配

c++ -::func has not been declared 错误

C++ 一个参数 bool 构造函数和 "new"关键字 : Logic Error

c++ - 线程安全的 cout 技术。我错过了什么吗?

algorithm - "cut and paste"std::vector 的最后 k 个元素有效吗?

c++ - 如何在程序代码中使用不可变对象(immutable对象)而不使用operator=()