Convert Const Char * to string

This is a discussion on Convert Const Char * to string within the C++ Programming forums, part of the General Programming Boards category; Code: #include <string> void Calc::yyerror(const char * mesg) { string x; // how can i make this to work ??? ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    41

    Convert Const Char * to string

    Code:
    #include <string>
    
    void
    Calc::yyerror(const char * mesg) {
    string x;
    
      // how can i make this to work ??? I would like to copy the mesg to the x string.
       x = mesg;
    
    
    }

  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    string x(mesg);

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,802
    Quote Originally Posted by winsonlee
    Code:
    #include <string>
    
    void
    Calc::yyerror(const char * mesg) {
    string x;
    
      // how can i make this to work ??? I would like to copy the mesg to the x string.
       x = mesg;
    
    
    }
    How can you make it work? It should work exactly as you have it written in your example above. One of the assignment operators for the string class takes a constant pointer to character as an argument.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    make sure you have "using namespace std;" or do this to declare the string:
    std::string x;
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  2. strcat - cannot convert char to const char
    By ulillillia in forum C Programming
    Replies: 14
    Last Post: 12-07-2006, 09:00 AM
  3. fatal error LNK1104
    By DMH in forum C++ Programming
    Replies: 2
    Last Post: 11-16-2005, 02:46 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21