开发者

None of the functions with this name in scope match the target type

I'm have two errors i need some help to fix

The error:

1>.\commands.cpp(1378) : error C2057: expected constant expression
1>.\commands.cpp(1378) : error C2466: cannot allocate an array of constant size 0
1>.\commands.cpp(1378) : error C2133: 'buffer' : unknown size

The code (commands.cpp):

 const uint32_t changeSexPrice = g_config.getNumber(ConfigManager::CHANGESEX_PRICE);

if(money < changeSexPrice)
{
    char buffer[70 + changeSexPrice];
    sprintf(buffer, "You do not have enough money. You need %d gold coins to change your sex.", changeSexPrice);
    player->sendCancel(buffer);
    return false;
}
enter code here

The error:

luascript.cpp(1752) : error C2664: 'lua_pushcclosure' : cannot convert parameter 2 from 'int32_t (__cdecl *)(lua_State *)' to 'lua_CFunction'
1>        None of the f开发者_运维知识库unctions with this name in scope match the target type

The code (luascript.cpp):

lua_register(m_luaState, "getSearchString", LuaScriptInterface::luaGetSearchString);

int32_t LuaScriptInterface::luaGetSearchString(lua_State* L)
{
//getSearchString(fromPosition, toPosition[, fromIsCreature[, toIsCreature]])
int32_t params = lua_gettop(L);
bool toIsCreature = false, fromIsCreature = false;

if(params >= 4)
    toIsCreature = popNumber(L) == LUA_TRUE;

if(params >= 3)
    fromIsCreature = popNumber(L) == LUA_TRUE;

PositionEx toPos, fromPos;
popPosition(L, toPos);
popPosition(L, fromPos);

if(toPos.x > 0 && fromPos.x > 0)
    lua_pushstring(L, g_game.getSearchString(fromPos, toPos, fromIsCreature, toIsCreature).c_str());
else
{
    reportErrorFunc("wrong position(s) specified.");
    lua_pushnumber(L, LUA_ERROR);
}
return 1;
}


You're trying to account for additional characters needed to store the number, but you'd overallocate anyway, so just make that buffer reasonably big (and use snprintf to avoid overflows), e.g.

char buffer[128];

As for the second error, you're trying to use a member function as a C callback. That won't work. Make that function either static or completely non-member, and if you need this pointer, find another way to pass it through (I don't know Lua API, so can't help you with that).


For the first area change your one line from this

char buffer[70 + changeSexPrice]

to this

char *buffer = new char[70 + changeSexPrice]

And then, just before the return statement, don't forget

delete[] buffer;

For the second part, I take you are attempting to pass a pointer to this function to lua_pushcclosure in another part of the code:

int32_t LuaScriptInterface::luaGetSearchString(lua_State* L) 

If that's the case, I think changing the return type to just int will do the trick, since `lua_cfunction is defined as

typedef int (*lua_CFunction) (lua_State *L);

Edit

Since we're using c++ here, this code is a very safe way to handle the buffer problem, no pointers or anything:

std::stringstream stream;
stream << "You do not have enought money. You need " << changeSexPrice << " gold coins to change your sex";
player->sendCancel(stream.str().c_str());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜