3

I'm trying to implement a class with similar behavior to std::string and I'm getting the error in the std::copy line:

Str& operator+=(const Str& s){
    std::copy(s.data.begin(), s.data.end(), std::back_inserter(data));
    return *this;
}

'data' is an object of type vec < char> , and vec is a vector-like class that I implemented myself and it seems to be working fine on its own.

It also says this:

C:\MinGW\bin..\lib\gcc\mingw32\3.4.2........\include\c++\3.4.2\bits\stl_iterator.h||In instantiation of `std::back_insert_iterator < vec< char> >':|

Koz
  • 151
  • 2
  • 14
  • Well does your `vec` have a `const_reference` type? – Simple Jan 30 '14 at 14:21
  • I added a 'typedef const T& const_reference' and it compiled, now let's see if it works. Thanks everyone for the answers, wish I could upvote you all. – Koz Jan 30 '14 at 14:26
  • You should accept one of the answers if they helped by clicking the tick. – Simple Jan 30 '14 at 14:28
  • Chapter 12 of "Accelerated C++", by A. Koenig & B. E. Moo. Would you please cite them? – Mattia Sep 10 '15 at 14:08

5 Answers5

4

It sounds like your vec doesn't meet the Container Requirements, so it's not guaranteed to be usable by standard facilities (such as back_inserter) that work with containers.

The requirements are specified in Table 96 in C++11, although Table 65 in C++98 is probably more appropriate for your ancient compiler. One of those requirements is a nested const_reference type.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
3

Check the requirements for std::back_inserter and std::copy. In particular, std::back_inserter expects an argument which fulfils the concept Container. At the very least this means implementing §23.2.1 of the standard, and one of the requirements listed there is:

X::const_reference | const lvalue of T | compile time

I.e. a typedef const_reference inside the container type.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
2

back_inserter is a convinience function which constructs a back_insert_iterator on the container; in this case, data.

data, you've said, is your own homegrown vector-type class. In order for this to work, your vector class must have a const_reference typedef defined. Something like this:

template <typename Item>
class Vec
{
public:
  typedef const Item& const_reference;
};

There are a number of other requirements for any implementation of a container. These are outlined in the C++03 Standard, in section 23.1 Container Requirements including Table 65.

See also this question for a discussion of the requirements.

Community
  • 1
  • 1
John Dibling
  • 99,718
  • 31
  • 186
  • 324
1

Try adding

typedef T value_type;
typedef const value_type& const_reference;

in your vec<T> body.

neverhoodboy
  • 1,040
  • 7
  • 13
0

Question needs some more details like your vec class.

What exactly is the error that you are getting? Please share more info on the error. console log would be helpful.

std::copy takes in two input iterators.(http://www.cplusplus.com/reference/algorithm/copy/) Are you sure that your vector like class is handling the iterators correctly?

Also please check if your vec support the container requirements needed by back_inserter. http://www.cplusplus.com/reference/iterator/back_inserter/

atuljangra
  • 457
  • 1
  • 5
  • 9
  • -1: A couple problems here. First, there are enough details presented to at least take a pretty close guess as to the problem. See the other answers. Second, this "answer" is really just a bunch of questions for the OP, asking for clarification. As such, this should be posted as a *comment*, not an *answer*. – John Dibling Jan 30 '14 at 14:31
  • I didn't have enough credits for posting the comment. – atuljangra Jan 31 '14 at 17:26