Help fixing C++ invalid conversion from const char* to char
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
#define _GLIBCXX_CSTRING 1
// Get rid of those macros defined in <string.h> in lieu of real functions.
#undef strrchr
and
Code:
_GLIBCXX_BEGIN_NAMESPACE(std)
using ::strrchr;
I suggest to first obtain preprocessed input file in order to better understand which of the implementations 'strrchr' is used.
The issue is moot in a sense there is a number of places where both 'const char *' and 'char *' are used.
So, you can also/first try to rewrite the code this way:
Code:
char *ext =
strrchr
(
NewName,
'.'
);
- in such a manner hopefully the compiler will point you to the exact place where the problem is, i.e. whether it dislikes conversion at 'NewName' or at 'ext = strrchr', i.e. whether it's the argument or the result.
I suggest to first obtain preprocessed input file in order to better understand which of the implementations 'strrchr' is used.
I just search the entire source tree and found a reference to "#include <string.h>" in several files, no references to cstring.h anywhere so I guess string.h it is.
Quote:
So, you can also/first try to rewrite the code this way:
Code:
char *ext =
strrchr
(
NewName,
'.'
);
- in such a manner hopefully the compiler will point you to the exact place where the problem is, i.e. whether it dislikes conversion at 'NewName' or at 'ext = strrchr', i.e. whether it's the argument or the result.
I tried this and it fails with "undelete.c:951: error: invalid conversion from ‘const char*’ to ‘char*’" on the " );" line.
I just search the entire source tree and found a reference to "#include <string.h>" in several files, no references to cstring.h anywhere so I guess string.h it is.
I tried this and it fails with "undelete.c:951: error: invalid conversion from ‘const char*’ to ‘char*’" on the " );" line.
Well, try brute force (i.e. type cast) then:
Code:
(char *)strrchr
- even if it works, it doesn't look clean. I.e. it's better to understand then what functionally for constant was intended.
Prototype for C strchr:
char *strchr(const char *s, int c);
Here's a reasonable solution:
Quote:
char *ext = strrchr((const char *)NewName, '.');
There are a number of stylist and/or technical issues one could debate (for example, using an "old fashioned" C-style cast instead of a C+ "const_cast<>") but, frankly, I think the above snippet should fix the problem, and be perfectly OK for you.
It seems to compile now, thank you. I'm not sure what the author is using a constant at all. I've tried emailing him but there was no response. Unfortunately I'm guessing the intent is something a more knowledgeable person then myself would have to look into.
There are a number of stylist and/or technical issues one could debate (for example, using an "old fashioned" C-style cast instead of a C+ "const_cast<>") but, frankly, I think the above snippet should fix the problem, and be perfectly OK for you.
'Hope that helps .. PSM
I tried your solution of char *ext = strrchr((const char *)NewName, '.'); but it actually gave me the same error as in the original post.
It seems to compile now, thank you. I'm not sure what the author is using a constant at all. I've tried emailing him but there was no response. Unfortunately I'm guessing the intent is something a more knowledgeable person then myself would have to look into.
Thanks again for the help!
Which of the two solutions worked - suggested by paulsm4 or myself ?
The approach is the same, the point is to understand what the compiler actually meant. I.e. to learn how to identify the exact problematic spot.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.