-1

I have a situation where I need to copy an array of pointers stored in a class to another class. Following the second answer to this question, I have created this reproducible example of what I am trying to do in my actual project.

#include <iostream>
#include <iterator>

struct A{};

class Something
{
public:
    static constexpr int nItems = 5;
    A* pA[nItems];
};

class AnotherThing
{
public:
    A* pACopy[Something::nItems];

    void CopyPointers(const A* pA[Something::nItems])
    {
        std::copy(std::begin(pA), std::end(pA), std::begin(pACopy));
    }
};

int main()
{
    Something something;
    AnotherThing anotherThing;
    anotherThing.CopyPointers(something.pA);
}

Error is in line std::copy(std::begin(pA), std::end(pA), std::begin(pACopy)); and the error is no instance of overload function "std::begin" matches the argument list. And its the same for std::end as well.

Cool_Cornflakes
  • 315
  • 2
  • 13

1 Answers1

2

void CopyPointers(const A* pA[Something::nItems]) is equivalent to void CopyPointers(const A** pA); pA is not an array but a pointer.

The fix is to use pointer arithmetic instead:

void CopyPointers(const A* pA[])
{
    std::copy(pA, pA + Something::nItems, std::begin(pACopy));
}
molbdnilo
  • 64,751
  • 3
  • 43
  • 82