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

青春壹個敷衍的年華 提交于 2019-12-12 04:53:21

问题


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:


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.



来源:https://stackoverflow.com/questions/26906589/how-to-fix-clang-libc-error-on-mac-calling-private-constructor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!