本文介绍了当我用g ++编译器编译c ++时,错误意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下代码:

#include<iostream>
#include<vector>

using namespace std;

int main()
{
    vector<int> ivec;
    for(vector<int>::size_type ix = 0; ix != 10; ix++)
    {
        ivec.push_back(ix);
    }
    vector<int>::iterator mid = (ivec.begin() + ivec.end()) / 2;
    cout << *mid << endl;
    return 0;
}

我用g ++编译错误:

I get an error compiling with g++:

iterator_io.cpp: In function `int main()':
iterator_io.cpp:13: error: no match for 'operator+' in '(&ivec)->std::vector<_Tp,               _Alloc>::begin [with _Tp = int, _Alloc = std::allocator<int>]() + (&ivec)->std::vector<_Tp, _Alloc>::end [with _Tp = int, _Alloc = std::allocator<int>]()'
/usr/lib/gcc/x86_64-redhat-linux/3.4.5/../../../../include/c++/3.4.5/bits/stl_iterator.h:654: note: candidates are: __gnu_cxx::__normal_iterator<_Iterator, _Container> __gnu_cxx::__normal_iterator<_Iterator, _Container>::operator+(const typename std::iterator_traits<_Iterator>::difference_type&) const [with _Iterator = int*, _Container = std::vector<int, std::allocator<int> >]
/usr/lib/gcc/x86_64-redhat-linux/3.4.5/../../../../include/c++/3.4.5/bits/stl_bvector.h:261: note:                 std::_Bit_iterator std::operator+(ptrdiff_t, const std::_Bit_iterator&)
/usr/lib/gcc/x86_64-redhat-linux/3.4.5/../../../../include/c++/3.4.5/bits/stl_bvector.h:345: note:                 std::_Bit_const_iterator std::operator+(ptrdiff_t, const std::_Bit_const_iterator&)
/usr/lib/gcc/x86_64-redhat-linux/3.4.5/../../../../include/c++/3.4.5/bits/stl_iterator.h:765: note:                 __gnu_cxx::__normal_iterator<_Iterator, _Container> __gnu_cxx::operator+(typename __gnu_cxx::__normal_iterator<_Iterator, _Container>::difference_type, const __gnu_cxx::__normal_iterator<_Iterator, _Container>&) [with _Iterator = int*, _Container = std::vector<int, std::allocator<int> >]

我知道ivec.end()不能用作普通的向量元素。但是我无法理解错误信息的含义...关于operator +的事情?

I know the ivec.end() can not be used as a normal vector element. But I can't understand what the error information means...something about operator+?

推荐答案

无法添加两个迭代器在一起。

运算符+ 没有为两个迭代器定义,因为该操作不会感。迭代器是指针的一种泛化 - 它们指向存储在容器中的特定元素。迭代器之和指向哪个元素?

operator+ is not defined for two iterators, because that operation wouldn't make sense. Iterators are a kind of generalization over pointers - they point to the specific element stored in container. At which element the sum of iterators is pointing?

但是,当使用向量时,可以将整数添加到迭代器,如下所示:

However, when you use a vector, you can add integers to iterators, like that:

vec.begin() + vec.size() / 2

这就是为什么你的候选人在你的错误信息中是:(...),然后是 operator + 的一些定义。

and that is why you have candidates are: (...) in your error message, followed by some definitions of operator+.

在你的情况下,最好,最干净的方法是不使用迭代器,但是简单从指定位置获取值

In your case the best, and cleanest way will be not using the iterators, but simple getting the value from specified position:

int mid = vec[vec.size() / 2];

这篇关于当我用g ++编译器编译c ++时,错误意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 06:03