Search code examples
c++typesenumsexpressionintegral

IntelliSense: expression must have integral or enum type


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

Solution

  • 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.