How to fix clang libc++ error on Mac: calling private constructor

3k views Asked by At

I'm trying to compile a (private) C++ software with Clang and libc++ on Mac OS X 10.10 and am getting this error:

error: calling a private constructor of class 'std::__1::__wrap_iter<unsigned short *>'

Full error message here.

Can someone explain this error and how to fix it? A small self-contained code example that results in this error and an option how to re-write it so that it works would be great!

1

There are 1 answers

3
Jonathan Wakely On BEST ANSWER

You're asking for a self-contained example showing the error but haven't provided your own example? That's not how stackoverflow works, you are meant to show the code not make people guess at the problem!

This produces the error:

#include <vector>

void f(unsigned short* p)
{
    std::vector<unsigned short>::iterator i(p);
}

Looks as though you're trying to construct an iterator from a pointer, which is not valid (it might work with some compilers, but is not portable).

You could try using pointer arithmetic to get the iterator instead:

std::ptrdiff_t d = std::distance(vec.data(), p);
std::vector<unsigned short>::iterator i = vec.begin() + d;

This assumes p really does point to an element of the vector, otherwise distance(vec.data(), p) is undefined.