3

I have created two arrays List1 and List2 with new in C++. List1 is updated with data. I would like to copy List1 to List2. I tried using

std::copy(std::begin(List1), std::end(List1), std::begin(List2));

However, this does not work and gives the following error message:

error: ‘begin’ is not a member of ‘std’

Any possible suggestions for this error? Or alternative ways to copy in C++?

12
  • @KerrekSB : Instead of new? I would not like to change this to std::vector, as the code is already under distribution. Commented Oct 8, 2013 at 16:41
  • 4
    std::begin is in <iterator>. They don't work on pointers, though. Commented Oct 8, 2013 at 16:42
  • @chris: already <iterator> is included Commented Oct 8, 2013 at 16:42
  • Do you use C++11? std::begin is from C++11. Commented Oct 8, 2013 at 16:42
  • 3
    Don't use begin and end then, just give it the first and one beyond the last members. std::copy(&List1[0], &List1[whatever_size_you_newed], List2); Commented Oct 8, 2013 at 16:51

3 Answers 3

2

"I have created two arrays List1 and List2 with new in C++"

The begin and end free functions won't work on a pointer.

Even if everything is correct, you may end up with no matching call to begin(T *&)

T is the type of your pointer.

Use std::vector or std::list other STL container or just simply static arrays to work with std::begin and std::end

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

7 Comments

Is there an explanation? or they just didn't include its support
@POW: Would you recommend using memcpy?
@khajvah See this, probably it answers your question
@SathishKrishnan that's why I asked what the arrays were. That will go wrong if the constructors need to do work
@khajvah The explanation is simply that a pointer is a pointer, not an array. A pointer can point to the start of an array (e.g., allocated with new[]), but there is no portable way to tell if it does, or how big that array is. This means that implementing end() is essentially impossible, and it would seem pointless to provide begin() without end().
|
0

Put this line towards the top of your cpp file:

#include <iterator>

Comments

0

To use std::begin and std::end you need to include <iterator> and make sure your compiler supports C++11. However this won't fix your problem, as you cannot use them with pointers. Use std::vector instead.

Comments

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.