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> >':|

4
  • Well does your vec<char> have a const_reference type? Commented Jan 30, 2014 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. Commented Jan 30, 2014 at 14:26
  • You should accept one of the answers if they helped by clicking the tick. Commented Jan 30, 2014 at 14:28
  • Chapter 12 of "Accelerated C++", by A. Koenig & B. E. Moo. Would you please cite them? Commented Sep 10, 2015 at 14:08

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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.

Comments

1

Try adding

typedef T value_type;
typedef const value_type& const_reference;

in your vec<T> body.

Comments

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/

2 Comments

-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.
I didn't have enough credits for posting the comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.