0

Guys i need someone fix this problem ? when i compile that code i have this error:

 Error: IntelliSense: expression must have integral or enum type

i have problem in this part:

Console(0, V("seta sv_hostname " + servername + ";\n"));

so how i can fix that

if (strncmp(command, V("exec config_mp"), 14) == 0)
{
    if (GAME_MODE == 'D')
    {
        CIniReader iniReader(V(".\\teknogods.ini"));
        char *servername = iniReader.ReadString(V("Settings"),V("Servername"),"");

        if (strcmp(servername,"") == 0)
        {
            info("Server name set to defult.");
        }
        else
        {
            //Console(0, V("seta scr_teambalance 1;\n"));
            Console(0, V("seta sv_hostname " + servername + ";\n"));
            info("server name set to: %s.", servername);
        }
    }
}
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
user1743737
  • 45
  • 1
  • 3
  • 5

1 Answers1

3

You cannot concatenate two C strings with +.

In C and C++ string literals are arrays of characters, which when used as rvalue in an expression decay into a pointer to the character. In C (and C++) you can perform pointer arithmetic, which means that you can add or substract an integer (or any integral type) from a pointer and you can also substract two pointers to obtain a difference, but you cannot add two pointers together. The expression "A" + "B" is incorrect as that would try to add two const char*. That is what the compiler is telling you: for the expression "seta sv_hostname " + servername to be correct, servername must be either an integer or an enum.

If coding C++ you can use std::string, for which there are overloaded operator+ that take either another std::string or const char* and then use the c_str member function to retrieve a const char* to use in interfaces that require C strings.

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
  • Thank you and can you tell me how i can convert servername to integer or an enum ?!! – user1743737 Oct 15 '12 at 18:15
  • @user1743737: You cannot convert a generic string to an integer and you don't want to do it either. You don't want pointer arithmetic, but string concatenation. If you want to concatenate strings, use `std::string` and then you can just concatenate: `std::string msg = "..."; msg += servername; msg += ";\n";`. To obtain a `const char*` you just call `c_str`: `msg.c_str()`. Now depending on what `Console` expects you might be able to pass a `std::string`, a `const char*` or neither of them... but without knowing the interface you use I cannot tell. – David Rodríguez - dribeas Oct 15 '12 at 20:18