Sign in to follow this  

const char* to char

This topic is 4078 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

My compiler(gcc) returns: wz_term.h:30: Fehler: ungültige Umwandlung von »const char*« in »char« in the following class. (Why????) I want txt to be a normal char class wz_term { private: char txt; public: float _x; float _y; int visible; void input(char); void backspace(); void execute(); wz_term(); }; wz_term::wz_term() { _x=10; _y=20; txt = "hallo"; char *line_0; line_0 = &txt visible = FALSE; //inside = "Welcome to wzTERM"; }; ... blablabla futher parts are here not needed

Share this post


Link to post
Share on other sites
you cant do that, there are 2 mistakes here, you are defining txt as a SINGLE char, and then trying to assign a string to it with the = operator.
a string is an array of chars, so you should declare it as such (IE: char txt[256];) or as a pointer which you will later allocate on the heap with new, and release with delete.

Then to actualy asign a value after declaration, you should use either sprintf,memcpy, or whatever the C++ function is.

Share this post


Link to post
Share on other sites
txt is a single character and you are trying to store a string in it. That is impossible. You have 3 choices. A character array in which has a constant size, eg:
class wz_term
{
private:
char txt[10];
public:
float _x;
float _y;
int visible;
void input(char);
void backspace();
void execute();
wz_term();
};
wz_term::wz_term()
{
_x=10;
_y=20;
strcpy(txt, "hallo");
char *line_0;
line_0 = txt
visible = FALSE;
//inside = "Welcome to wzTERM";
};


A char pointer which you initialize with memory(don't forget to delete it in the constructor!):


class wz_term
{
private:
char * txt;
public:
float _x;
float _y;
int visible;
void input(char);
void backspace();
void execute();
wz_term();
};
wz_term::wz_term()
{
_x=10;
_y=20;
txt = new char[10];
strcpy(txt, "hallo");
char *line_0;
line_0 = txt
visible = FALSE;
//inside = "Welcome to wzTERM";
};


or you can leave that completely behind and use std::string (in the header file string)


class wz_term
{
private:
std::string txt;
public:
float _x;
float _y;
int visible;
void input(char);
void backspace();
void execute();
wz_term();
};
wz_term::wz_term()
{
_x=10;
_y=20;
txt = "hallo";
// const char *line_0 = txt.c_str(); // this would 'work' but probably not as it should
std::string lin_0 = txt;
visible = FALSE;
//inside = "Welcome to wzTERM";
};


As you can see. std::string is far easier than whatever it was you were trying to do.

Share this post


Link to post
Share on other sites
This is inconsistent:
char txt;
// ...
txt = "hallo";
char *line_0;
line_0 = &txt
(As an aside, there's a missing ';' after &txt, which suggests you either didn't post the exact code, or didn't post all the errors.)

For 'txt = "hallo"; to be valid, txt must be a char* or a const variation thereof. Once you've made that change, you'll also need to change the last line to 'line_0 = txt;'.

If you post the code again with changes, use [ source ][ /source ] tags (without the spaces) so that it's easier to read. Also, when posting errors it's easier to follow if the posted code matches the file exactly so that the line numbers correspond, and/or you add a comment in the file itself showing where the error occurs.

[Ha, beaten by three posts.]

Share this post


Link to post
Share on other sites

This topic is 4078 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this