views:

84

answers:

3

Im trying to compile but im getting these errors:

1>.\item.cpp(123) : warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)

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

Line 123 item.cpp

if((bool)random_range(0, 1))

Line 1372 commands.cpp

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;
}

any idea?

+2  A: 

Your problem is char buffer[70 + changeSexPrice];. win32 compilers need a constant expression when doing stack allocation.

I'm not sure why you're adding changeSexPrice as you are only using the buffer to print an int. I bet if you pick a something like char buffer[1024] then you will have more than enough for your needs.

EDIT: Per the comments (which are very good).

If you use a fixed size buffer of len 1024, use snprintf. In Visual Studio's case, this is sprintf_s. Your code would change to:

sprintf_s(buffer, 1024, "You don't have enough money ...", yourValueHere);

Alternatively, Mark B presents an answer that removes the need for your own mem allocation as well.

SB
Or, since this is C++, use `std::string` and avoid manual memory allocation altogether.
Mike Seymour
If you're going to suggest a fixed buffer size at least insist on `snprintf`. You never know when code will be copied and/or change and whatever fixed length you allocated is not enough.
Mark B
Thanks for the comments. Updated accordingly.
SB
A: 
if((bool)random_range(0, 1))

Better write something like:

if(random_range(0, 1) == 1)

This is clearer, and the warning will disappear.

Thomas
better (and probably more correct) is `if (random_range(0, 1))`
Alexandre C.
Not better, because the result of `random_range` is a coin flip and does not, by itself, make sense as a boolean value: you have to ask "heads or tails?" first. Not more correct, because it does something different to the original code. (N.B. I'm assuming that `random_range` produces integer values. If it produces floating-point values, this code is broken anyway.)
Thomas
@Thomas: actually, Alexandre's code does exactly the same as the original, as would `if (random_range(0,1) != 0)`. Yours only does the same if the function is known to only return `0` or `1`.
Mike Seymour
Hmm. You're right, and I was being stupid, sorry.
Thomas
+1  A: 

Declaring variable length stack-based arrays is a gcc extension (and possibly others too). Use an ostringstream to do your formatting:

if(money < changeSexPrice)
{
    std::ostringstream os;
    os << "You do not have enough money. You need " << changeSexPrice << " gold coins to change your sex.";
    player->sendCancel(os.str().c_str());   // This assumes that sendCancel doesn't take ownership of its parameter.
    return false;
}
Mark B
Regarding the assumption: the original code makes the same assumption.
Mike Seymour