I have a function which is expecting a string, I was wanting to concatenate const char * to a string to be returned.
Here is example code to help illustrate this scenario:
void TMain::SomeMethod(std::vector<std::string>* p)
{
p->push_back(TAnotherClass::Cchar1 + "/" + TAnotherClass::Cchar2);
}
and here is the other class these are from:
class TAnotherClass
{
public:
static const char * Cchar1;
static const char * Cchar2;
};
const char * TAnotherClass::Cchar1 = "Home";
const char * TAnotherClass::Cchar2 = "user";
im getting the following error:
invalid operands of types ‘const char*’ and ‘const char*’ to binary operator +
Why is this not valid? please help
char const*cannot be used with+operator, as the error says.What you need to do is this:
It creates a temporary object of type
std::string, then you can use+with it. It concatenates the strings, creating temporaries all the way, and finally passes the final string topush_back.Apart from that, as @Konrad noted in the comment, don’t pass a pointer into the method, use a reference instead, as: