I'm trying to wrap a class from a library I'm using in Lua. Specifially, I'm trying to wrap the color class from SFML. The full sou开发者_运维知识库rce for the color class can be seen here and here.
This is the function that's that I'm failing in.
int SFColor_new(lua_State* L)
{
// omitting part where I set r, g, b, and a
new (lua_newuserdata(L, sizeof(Color))) Color(r, g, b, a); // Line 47
luaL_getmetatable(L, LuaInfo<Color>::myMetaTableName);
lua_setmetatable(L, -2);
return 1;
}
And this is the error
LuaSFMLColor.cpp: In function ‘int ag::SFColor_new(lua_State*)’:
LuaSFMLColor.cpp:47: error: no matching function for call to ‘operator new(unsigned int, void*)’
<built-in>:0: note: candidates are: void* operator new(unsigned int)
make: *** [game] Error 1
I do similar things in a few other places without running into this error, so I'm not sure what would be causing it. Looking at the Color's source code I don't see anything weird or out of the ordinary, and I've run out of ideas. I've also tried using the default constructor (i.e. no arguments) and then just setting the value afterward, but that didn't do any good either.
To use the standard placement form of new
you have to #include <new>
.
The form of new
that you are using requires a declaration of void* operator new(std::size_t, void*) throw();
.
You don't have to #include <new>
to use non-placement new
.
The question has already been answered, but here's possible solution if you did #include <new>
and still get error: no matching 'operator new' function for non-allocating placement new expression; include <new>
on clang.
My code looked like this:
int* fun(const std::array<std::byte, 123>& param, std::size_t offset) {
return new(param.data()+offset) int;
}
When I switched to GCC, I got error: invalid conversion from 'const void*' to 'void*
, which is significantly more useful. param
must be passed by non-const reference.
It might seem obvious, but the original code was much more complex and it was difficult to spot the actual source of the issue.
精彩评论