const char* to char

Started by
3 comments, last by Zakwayda 17 years, 6 months ago
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
May the force be with... me! :-D
Advertisement
"txt" is a char, not a char* (or std::string, which is what you should be using). A string literal in C++ is a const char*. Your types are incompatible.

Use std::string.
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.
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.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

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.]

This topic is closed to new replies.

Advertisement