当我用g ++编译器编译c ++时,错误是什么意思? [英] what does the error mean when I am compiling c++ with g++ complier?

查看:127
本文介绍了当我用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 ++编译时出现错误:

  iterator_io.cpp:在函数`int main()'中:
iterator_io.cpp:13:错误:'operator +'in' & ivec)> std :: vector< _Tp,_Alloc> :: begin [with _Tp = int,_Alloc = std :: allocator< int>]()+(& ; _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:注意:候选者是:__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& :allocator< int> >]



我知道ivec.end()不能用作法线向量元素。但是我不能理解错误信息是什么意思...关于operator +的一些事情。

解决方案

迭代器。



没有为两个迭代器定义运算符+ ,因为该操作不会感。迭代器是对指针的一种泛化 - 它们指向存储在容器中的特定元素。但是,当使用向量时,您可以向迭代器添加整数,例如:



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

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



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

  int mid = vec [vec.size 2]。 


using the following code:

#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;
}

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> >]

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+?

解决方案

You cannot add two iterators together.

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

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 ++时,错误是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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