Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does the error mean when I am compiling c++ with g++ compiler?

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

like image 920
yangfei1831 Avatar asked Mar 13 '12 13:03

yangfei1831


People also ask

What is compiler error in C?

Compilation error refers to a state when a compiler fails to compile a piece of computer program source code, either due to errors in the code, or, more unusually, due to errors in the compiler itself. A compilation error message often helps programmers debugging the source code.

Why am I getting a compiler error?

A compile error happens when the compiler reports something wrong with your program, and does not produce a machine-language translation. You will get compile errors. Everybody does. Don't let them bother you.

What is GCC compiler error?

The name of the assembler program with gcc is just as . So the error message tells you, that running the assembler fails, because the assembler executable contains an illegal instruction. This might really be an hardware error, meaning that the executable of the assembler is broken.


3 Answers

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]; 
like image 107
Rafał Rawicki Avatar answered Sep 28 '22 10:09

Rafał Rawicki


You cannot add iterators. What you can do:

vector<int>::iterator mid = ivec.begin() + distance(ivec.begin(), ivec.end()) / 2;
like image 30
Henrik Avatar answered Sep 28 '22 08:09

Henrik


It simply means that vector iterators have no addition operator (+). You cannot add ivec.begin() and ivec.end().

To get the middle element, you can simply use the subscript operator:

cout << ivec[ivec.size()/2] << endl;

If you insist on using an iterator, you can get an iterator that points to the middle in this fashion:

vector<int>::iterator mid = ivec.begin();
mid += ivec.size()/2;
cout << *mid << endl;

You can do this, because the vector iterator is a random access iterator (in all implementations I'm aware of it encapsulates an actual pointer to the raw container-data).

like image 26
bitmask Avatar answered Sep 28 '22 08:09

bitmask