I get the following error:
2 smartcard.c: In member function ‘virtual bool cSmartCards::ParseLine(const char*, bool)’:
3 smartcard.c:1187:25: error: invalid conversion from ‘const char*’ to ‘char*’ [-fpermissive]
4 char *r=index(line,':');
5 ^
The code is:
1184
1185 bool cSmartCards::ParseLine(const char *line, bool fromCache)
1186 {
1187 char *r=index(line,':');
1188 if(!r) return false;
I included "string.h"
How can I rewrite line 1187?
index() can be found in string.h.
Either or both of the following:
index returns const char*, not a char*. So, make r a const char*, not a char*.
The function index is written to expect a char*, not a const char*. I cannot safely suggest a fix for this without knowing what index is and does.
The answer is the following:
Function index is found in string.h.
Change line 1187 to: const char *r=index(line,':');
Related
I read about this issue (seems quite common) for hours without finding any applicable solution to my situation. I understand that there might be a problem related to the included files and libraries, but I'm not actually able to find what's wrong.
It's an Arduino script that uses the MQTT client library. It works beautifully with the old PubSubClient.h lib.
Now I wanted to update the lib with a recent one. The function definitions are changed so I made the changes in the sketch and switched the lib in the Arduino/library directory, then restarted the Arduino IDE. I get lot of "error: 'argument' does not name a type" and I really don't know what to fix.
Here you can find the .h and .cpp files organization to understand what's happening.
//device.ino file
#include "device.h"
//device.h file
#ifndef DEVICE_H
#define DEVICE_H
#include <WiFiClient.h>
#include <WiFiClientSecure.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <ESP8266HTTPUpdateServer.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include <jsmn.h>
#include <Arduino.h>
#include <pgmspace.h>
#include "mqtt.h"
#endif
mqtt.h file
#ifndef MQTT_H
#define MQTT_H
#include "device.h"
#endif
mqtt.cpp file
#include "mqtt.h"
WiFiClient wifi_client;
PubSubClient mqtt_client(wifi_client);
mqtt_client.setServer(mqtt_server, MQTT_BROKER_PORT);
mqtt_client.setCallback(mqtt_callback);
Finally the errors the compiler throws out:
mqtt.cpp:19: error:
'mqtt_client' does not name a type
mqtt_client.setServer(mqtt_server, MQTT_BROKER_PORT);
^
mqtt.cpp:20: error: 'mqtt_client' does not name a type
mqtt_client.setCallback(mqtt_callback);
^
sketch\mqtt.cpp: In function 'void mqtt_publish_mex(String, String, bool)':
mqtt.cpp:27: error: no matching function for call to 'PubSubClient::publish(String&, String&, bool&)'
if (mqtt_client.publish(topic, jmex, retained)) {
^
sketch\mqtt.cpp:27:54: note: candidates are:
In file included from sketch\Walvola.h:25:0,
from sketch\mqtt.h:4,
from sketch\mqtt.cpp:1:
C:\Users\Fabrizio & Dario\Documents\Arduino\libraries\pubsubclient\src/PubSubClient.h:130:12: note: boolean PubSubClient::publish(const char*, const char*)
boolean publish(const char* topic, const char* payload);
^
C:\Users\Fabrizio & Dario\Documents\Arduino\libraries\pubsubclient\src/PubSubClient.h:130:12: note: candidate expects 2 arguments, 3 provided
C:\Users\Fabrizio & Dario\Documents\Arduino\libraries\pubsubclient\src/PubSubClient.h:131:12: note: boolean PubSubClient::publish(const char*, const char*, boolean)
boolean publish(const char* topic, const char* payload, boolean retained);
^
C:\Users\Fabrizio & Dario\Documents\Arduino\libraries\pubsubclient\src/PubSubClient.h:131:12: note: no known conversion for argument 1 from 'String' to 'const char*'
C:\Users\Fabrizio & Dario\Documents\Arduino\libraries\pubsubclient\src/PubSubClient.h:132:12: note: boolean PubSubClient::publish(const char*, const uint8_t*, unsigned int)
boolean publish(const char* topic, const uint8_t * payload, unsigned int plength);
^
C:\Users\Fabrizio & Dario\Documents\Arduino\libraries\pubsubclient\src/PubSubClient.h:132:12: note: no known conversion for argument 1 from 'String' to 'const char*'
C:\Users\Fabrizio & Dario\Documents\Arduino\libraries\pubsubclient\src/PubSubClient.h:133:12: note: boolean PubSubClient::publish(const char*, const uint8_t*, unsigned int, boolean)
boolean publish(const char* topic, const uint8_t * payload, unsigned int plength, boolean retained);
^
C:\Users\Fabrizio & Dario\Documents\Arduino\libraries\pubsubclient\src/PubSubClient.h:133:12: note: candidate expects 4 arguments, 3 provided
sketch\mqtt.cpp: In function 'void mqtt_log(String)':
mqtt.cpp:347: error: no matching function for call to 'PubSubClient::publish(const String&, String&)'
mqtt_client.publish(mqtt_controllers_topic_debug, json_string);
^
sketch\mqtt.cpp:347:70: note: candidates are:
In file included from sketch\Walvola.h:25:0,
from sketch\mqtt.h:4,
from sketch\mqtt.cpp:1:
C:\Users\Fabrizio & Dario\Documents\Arduino\libraries\pubsubclient\src/PubSubClient.h:130:12: note: boolean PubSubClient::publish(const char*, const char*)
boolean publish(const char* topic, const char* payload);
^
C:\Users\Fabrizio & Dario\Documents\Arduino\libraries\pubsubclient\src/PubSubClient.h:130:12: note: no known conversion for argument 1 from 'const String' to 'const char*'
C:\Users\Fabrizio & Dario\Documents\Arduino\libraries\pubsubclient\src/PubSubClient.h:131:12: note: boolean PubSubClient::publish(const char*, const char*, boolean)
boolean publish(const char* topic, const char* payload, boolean retained);
^
C:\Users\Fabrizio & Dario\Documents\Arduino\libraries\pubsubclient\src/PubSubClient.h:131:12: note: candidate expects 3 arguments, 2 provided
C:\Users\Fabrizio & Dario\Documents\Arduino\libraries\pubsubclient\src/PubSubClient.h:132:12: note: boolean PubSubClient::publish(const char*, const uint8_t*, unsigned int)
boolean publish(const char* topic, const uint8_t * payload, unsigned int plength);
^
C:\Users\Fabrizio & Dario\Documents\Arduino\libraries\pubsubclient\src/PubSubClient.h:132:12: note: candidate expects 3 arguments, 2 provided
C:\Users\Fabrizio & Dario\Documents\Arduino\libraries\pubsubclient\src/PubSubClient.h:133:12: note: boolean PubSubClient::publish(const char*, const uint8_t*, unsigned int, boolean)
boolean publish(const char* topic, const uint8_t * payload, unsigned int plength, boolean retained);
^
C:\Users\Fabrizio & Dario\Documents\Arduino\libraries\pubsubclient\src/PubSubClient.h:133:12: note: candidate expects 4 arguments, 2 provided
In file included from sketch\mqtt.h:4:0,
from sketch\mqtt.cpp:1:
sketch\mqtt.cpp: In function 'void mqtt_callback(char*, byte*, unsigned int)':
mqtt.cpp:375: error: request for member 'payload_string' in 'payload', which is of non-class type 'byte* {aka unsigned char*}'
log(payload.payload_string());
^
sketch\Walvola.h:89:67: note: in definition of macro 'log'
#define log(mex) if (DEBUG) {Serial.println(walvola_time + "::" + mex);Serial.flush(); if(MQTT_DEBUG) {mqtt_log(walvola_time + "::" + mex);}}
^
mqtt.cpp:375: error: request for member 'payload_string' in 'payload', which is of non-class type 'byte* {aka unsigned char*}'
log(payload.payload_string());
^
sketch\Walvola.h:89:135: note: in definition of macro 'log'
#define log(mex) if (DEBUG) {Serial.println(walvola_time + "::" + mex);Serial.flush(); if(MQTT_DEBUG) {mqtt_log(walvola_time + "::" + mex);}}
^
mqtt.cpp:379: error: request for member 'payload_string' in 'payload', which is of non-class type 'byte* {aka unsigned char*}'
if (mqtt2JSONkvs(payload.payload_string())) {
^
mqtt.cpp:404: error: a function-definition is not allowed here before '{' token
{
^
mqtt.cpp:512: error: expected '}' at end of input
}
^
Più di una libreria trovata per "WiFiClient.h"
Usata: C:\Users\Fabrizio & Dario\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\libraries\ESP8266WiFi
Non usata: C:\Program Files (x86)\Arduino\libraries\WiFi
Non usata: C:\Program Files (x86)\Arduino\libraries\WiFi
Non usata: C:\Program Files (x86)\Arduino\libraries\WiFi
Non usata: C:\Program Files (x86)\Arduino\libraries\WiFi
exit status 1
'mqtt_client' does not name a type
Thanks for the help!
when writing
WiFiClient wifi_client;
PubSubClient mqtt_client(wifi_client);
mqtt_client.setServer(mqtt_server, MQTT_BROKER_PORT);
mqtt_client.setCallback(mqtt_callback);
the first two lines are quite ok => you are declaring global variables named wifi_client and mqtt_client
The problem comes with the calls
mqtt_client.setServer(mqtt_server, MQTT_BROKER_PORT);
mqtt_client.setCallback(mqtt_callback);
You can declare variables at global scope, but you cannot call functions outside a function (unless is for a global variable initialization), this is why you get this error. The example you point is different as the variables are in a function, thus calling methodes on them just after is legal: you are in a function context.
So you need to move your calls in the main() function, for example, so that the code gets valid.
That said, putting global variables in the header could make you more trouble is you include you file from different cpp files, the compiler will complain about multiple definitions, but that is not (yet) the question here.
I have a huge list of errors that come up when i try to compile source code under cygwin.. My best approach to learning programming is hitting it hard, and trail and error. So even if my C++ knowledge is very basic, I am still really new, so please when you explain can I please ask that you use baby talk for a lack of a better word lol. When I type in 'make' under the source directory is gives me these errors. A friend of mine, we are friends on a MUD, he has been a programmer for 35 years and he says to me that the compiler is not liking that the function is returning a pointer and to change all the "return ''''" to return strdup('''')
Please let me know what you guys think. Thanks
Underneath is only a very smalllll part of the syntax that was given to me after I typed in make in Cygwin. I hope someone has the time to explain this to me, thank you.
$ make
make -s smaug
-Compiling o/imc.o....
imc.c:106:1: error: deprecated conversion from string constant to ‘char*’ [-Werror=write-strings]
};
^
imc.c:106:1: error: deprecated conversion from string constant to ‘char*’ [-Werror=write-strings]
imc.c:106:1: error: deprecated conversion from string constant to ‘char*’ [-Werror=write-strings]
imc.c:106:1: error: deprecated conversion from string constant to ‘char*’ [-Werror=write--strings]
imc.c:106:1: error: deprecated conversion from string constant to ‘char*’ [-Werror=write-strings]
imc.c:106:1: error: deprecated conversion from string constant to ‘char*’ [-Werror=write-strings]
imc.c: In function ‘char* color_itom(const char*, CHAR_DATA*)’:
imc.c:393:14: error: deprecated conversion from string constant to ‘char*’ [-Werror=write-strings]
return "";
^
imc.c: In function ‘char* color_mtoi(const char*)’:
imc.c:414:14: error: deprecated conversion from string constant to ‘char*’ [-Werror=write-strings]
return "";
^
imc.c: In function ‘char* imccapitalize(const char*)’:
imc.c:525:35: error: conversion to ‘char’ from ‘int’ may alter its value [-Werror=conversion]
strcap[i] = tolower( str[i] );
^
imc.c:527:35: error: conversion to ‘char’ from ‘int’ may alter its value [-Werror=conversion]
strcap[0] = toupper( strcap[0] );
^
imc.c: In function ‘void imc_new_channel(const char*, const char*, const char*, const char*, const char*, bool, int, const char*)’:
imc.c:1089:13: error: conversion to ‘short int’ from ‘int’ may alter its value [-Werror=conversion]
c->level = perm;
^
^
cc1plus: all warnings being treated as errors
Makefile:101: recipe for target 'o/imc.o' failed
make[1]: *** [o/imc.o] Error 1
Makefile:46: recipe for target 'all' failed
make: *** [all] Error 2
Ok below is the code where it shows error for line 106: 1 and 393: Its a very lonnng .c file I am sure you guys don't want to upload the whole thing, but here is the portion of it, and according to Visual 2013 here is starting point line 106 and 393: i am not sure when cygwin says the line number where the error took place if that doesn't include white space and comments, but here is 106 and 393 according to VS:
line 106
SITEINFO *this_imcmud;
line 393
if( IMCIS_SET( IMCFLAG( ch ), IMC_COLORFLAG ) )
You should show your code, but your problems are:
Somewhere you're doing something like:
char *x = "hello";
It should be:
const char *x = "hello";
Similarly, char* color_itom(const char*, CHAR_DATA*) should return const char * if you want to return string literals from it.
strcap is defined as a char array, but you're putting the int values returned by tolower and toupper in there somewhere. Either change the type or put in an explicit cast.
Same for c->level = perm. Either add an explicit cast or change the type of c->level to match the type of perm.
The error:
deprecated conversion from string constant to ‘char*’
is caused by using string literal to initiaize char*, e.g.
char* str = "something";
This should read:
const char* str = "something";
Where am doing wrong in this code? I need only in char types, please don't suggest to use std::string.
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char *mystring="C:/windows";
char last_char;
last_char = mystring[strlen(mystring)-1];
cout<<"Input: " <<mystring<<endl;
if(strcmp(last_char,";")!=0)
{
strcat(mystring,";");
}
cout<<"Output: "<<mystring<<endl;
return 0;
}
Output:
Compilation error time: 0 memory: 3340 signal:0
prog.cpp: In function ‘int main()’:
prog.cpp:7:17: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
char *mystring="C:/windows";
^
prog.cpp:11:25: error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]
if(strcmp(last_char,";")!=0)
^
In file included from prog.cpp:2:0:
/usr/include/string.h:140:12: error: initializing argument 1 of ‘int strcmp(const char*, const char*)’ [-fpermissive]
extern int strcmp (const char *__s1, const char *__s2)
Don't use strcmp, it expects a null terminated characters sequence. Instead, use direct comparison:
if (last_char == ';') ...
Also, your code invokes undefined behavior in the strcat() call. my_string was initialized with a string literal, thus, you are not allowed to modify it, since the implementation is free to place it in read-only memory (and typically will do so).
You can declare it like this instead:
char mystring[12] = "C:/windows"; // space for one more char
last_char is not a string. It is a character. You can't compare a char with string.
Try this instead
if (last_char == ';') {...}
Statement
strcat(mystring,";");
invokes undefined behavior. You can't modify a string literal as it resides in read only section of the memory.
I'm trying to search for a word from a file using the boost module and c++ and I'm stuck on this error message:
error: cannot convert 'const value_type* {aka const wchar_t*}' to 'const char*' in initialization
The error comes from this line of code:
const char* file_path = itr->path ().filename ().c_str();
Thanks.
It is telling you you need this:
const wchar_t* file_path = itr->path ().filename ().c_str();
I'm having a problem with strcmp.
This is my code.
while (strcmp("m",wood) !=0 || strcmp("j",wood) !=0 || strcmp("o",wood) !=0){
cout << "(m for mahogany, o for oak, or p for pine): ";
cin >> wood;
}
And this is my error:
dining.cpp: In member function ‘void DiningSet::woodType()’:
dining.cpp:76:24: error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]
/usr/include/string.h:143:12: error: initialising argument 2 of ‘int strcmp(const char*, const char*)’ [-fpermissive]
dining.cpp:76:48: error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]
/usr/include/string.h:143:12: error: initialising argument 2 of ‘int strcmp(const char*, const char*)’ [-fpermissive]
dining.cpp:76:72: error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]
/usr/include/string.h:143:12: error: initialising argument 2 of ‘int strcmp(const char*, const char*)’ [-fpermissive]
wood is of type char: it must be a string, ie, char*, to be used in strcmp().
Change to:
while ('m' != wood && 'j' != wood && 'o' != wood)
The error shows problem with second argument. It should be of a const char *
The signature of strcmp is:
int strcmp ( const char * str1, const char * str2 );