Xyber 101
Xyber 101

Reputation: 3

I'm getting a compile time error when appending a string to a const char* in C++?

so I'm trying to convert an integer value into a string and then append the string to an already existing const char*. But as mentioned, I'm getting this error:

error: expression must have integral or unscoped enum type.

 objDim += objDimStr.c_str();

Any ideas why? My code is as follows:

    const char* objDim = "The object dimension is: ";
    int objDimension = geo->Dimension();
    std::string objDimStr = std::to_string(objDimension);
    objDim += objDimStr.c_str();

Upvotes: 0

Views: 217

Answers (4)

serge
serge

Reputation: 1022

objDim += objDimStr.c_str();

Here you add one pointer to other instead of concatenating the string. You need use std::string to concatenate, for example:

const char* objDim = "The object dimension is: ";
int objDimension = geo->Dimension();
std::string objDimStr = objDim;
objDimStr += std::to_string(objDimension);
std::cout << objDimStr << std::endl;

Upvotes: 0

Some programmer dude
Some programmer dude

Reputation: 409176

objDim is a pointer. When you add to the pointer, you will add to the pointer itself and not append anything to the data it points to.

And you can not add everything to a pointer, basically only integer values. And adding two pointers like you do makes no sense at all (which is why it's not allowed).

If you want to be able to append strings, then use std::string for all strings.

Besides, that you need to use the const qualifier should also give some hints about that what the pointer is pointing to is constant and therefore can't be modified (including added to).


And in this case you don't need any of the temporary variable objDimension or objDimStr (unless you use them later in the code):

std::string objDim = "The object dimension is: " + std::to_string(geo->Dimension());

Here the addition works because there is an overloaded operator+ function that takes the correct arguments in the correct order, and returns a new std::string object.

Upvotes: 2

bipll
bipll

Reputation: 11940

I assume you were expecting to get a concatenation of strings. No, it's not how it works. objDim is but a pointer to a non-modifiable memory area that contains characters. The only possible application of operator += to it that would compile is if you were incrementing it by a few characters:

objDim += 11;
std::cout << objDim << '\n'; // prints "dimension is: ";

To concatenate strings you can:

  1. Either assign the result to a string object:

    std::string result = objDim + objDimStr;
    

    (Note the absence of c_str anywhere: you cannot sum two pointers, but there's an overload that can prepend a char pointer to a std::string.)

  2. Or give proper type to objDim;

    std::string objDim{"The object dimension is: "};
    objDim += objDimStr;
    

Upvotes: 2

P.W
P.W

Reputation: 26800

objDim is not an std::string but a pointer, so your concatenation operator (+) which is defined for std::string will not work on it.

Define it as an std::string and you will be able to use it to concatenate other std::strings to it.

Upvotes: 1

Related Questions