I've already gone through the following links but couldn't get it to work:
Adding to c* invalid conversion from const char* to char*"
Why is conversion from string constant to 'char*' valid in C but invalid in C++
I'm working on arduino Uno. I need to send an argument of type char*
to a function. I have the following code:
const char* topic = "SampleTopic";
const char* msg = "Hello";
publish(0, 0, 0, "msgid", topic, msg);
I'm getting this error:
initializing argument 5 of 'void GSM_MQTT::publish(char, char, char, unsigned int, char*, char*)'
void publish(char DUP, char Qos, char RETAIN, unsigned int MessageID, char *Topic, char *Message);
warning: invalid conversion from 'const char*' to 'char*' [-fpermissive]
publish(0, 0, 0, _generateMessageID(), topic, msg);
^
I've even tried using const std::string& topic = "SampleTopic";
but get this error:
'string' in namespace 'std' does not name a type
Even const char* topic = (char*)"SampleTopic";
and passing it as func(topic)
gives the same error.
How can I resolve this ??
std::string
error, Arduino may use C++ but IIRC it doesn't use the standard C++ library. Instead it have its own class-library, where you have for example aString
class. – Some programmer dudeconst
anything to non-const. You could castconst
away, but it's almost always a bad idea. The error message, by the way, contains a hint about why you get the warning (you pass aconst char *
to a function that expects achar *
). – Some programmer dudepublish
does not modify the strings pointed to byTopic
andMessage
, you could use an ugly cast to cast away theconst
:publish(0, 0, 0, (char *)topic, (char *)msg);
. Ifpublish
is under your control, it would be better to changepublish
to useconst char *
parameters. – Ian Abbott(char*)"Topic Name"
but it gives meISO C++ forbids converting a string constant to 'char*'
– mrid