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.
Seems fine to me. Can you show us the rest of the code and the exact error message and verify that the error is from this function? Maybe it's from the code that calls this function?
i m sorry but i forgot to tell about the error message..
in the function defination
u see that i have declared
const char* x;
if i deleted the const word ,the error will appear..
invalid conversion from const char* to char*
Since you're using C++ I strongly recommend std::string instead of char* or const char*. You can always pass an std::string.c_str() to library functions which need const char* arguments and it's much less hassle to use std::string in all your programs since you needn't allocate and deallocate memory each time like you do for char*.
edit: since you're already using namespace std, it should be a matter of just declaring it as
string a_str = "A string";
Last edited by vharishankar; 08-04-2006 at 06:42 AM.
You declared current as const char* so you have to declare x also as const, because there is no impicite conversion from const char* to char* (exactly what the error message told you). If you necessarily need to assign a "const" to a "non-const", then you can use the explicite convert operator const_cast:
Code:
const char *current;
char *x;
x = const_cast<char*>(current);
But be careful; very often you should consider to change your design instead of using const_cast. You may also want to go through some tutorials about type casting as well as the const modfier in C++. Simply ask google for these words.
PS. And as Harishankar said already: You should consider to use std::string
thx everyone .. i always use casting to solve this problem , but i m asking about another kind of solution..i mean is there a flag i pass to g++ which elminate reporting this kind of error?
Const correctness is a good thing. You are making a promise that you are not going to modify the stuff something points to, etc. So either you keep the promise, or you don't make it in the first place. What's so hard about that?
but call: meta_data::decode_int( const char* current ,int* charecter_lenght , long* integer )
and get: invalid conversion from `const char*' to `char*'
and for me it should be: invalid conversion from `char*' to `const char*'
It should not be a proble with const char to char but a problem with char to const char.
Strange?
No, you can always do that. Do you understand what the const means? "const char *" means you cannot do anything that can modify the things pointed to by the pointer, which is more restrictive than "char *", so it's always okay to convert from "char *" to "const char *", but not the other way.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.