Results 1 to 18 of 18

Thread: String.....

  1. #1

    Thread Starter
    Addicted Member Virtual24's Avatar
    Join Date
    May 2001
    Posts
    228

    String.....

    I am having problems using the string variable...

    I am using it like this:

    Code:
    void ErrorMsg( string emsg )
    {
        MessageBox( emsg, "", MB_OK );
    }
    it give me the error: MessageBoxA cannot convert basic_string to const char *.

    How do i use a string variable for this kind of situation... There are other functions that i am trying to do this with that all give the same error... Can anyone help???
    To protect time is to protect everything...

  2. #2
    Lively Member
    Join Date
    Oct 2001
    Posts
    80
    Not sure if you can do it in this situation (I never worked with string type), but you can try casting:

    Code:
    void ErrorMsg( string emsg )
    {
        MessageBox( (char *)emsg, "", MB_OK );
    }
    Casting is basically taking a variable type and converting it to another variable type (if it's possible). Sometimes this can cause an error, but other times you can't do without it. Other then that you could just write a function that converts a string to char *. Something along the lines of: get the first char in string and put it into char[0], get the second and put it into char[1] and so on.

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    string has a function that will return a const char*: c_str()
    Yo can't get a buffer out of a string, you must create one the usual way
    Code:
    char buf[100];  // A buffer that can hold a string of 99 characters at max.
    // Then call a function:
    GetModuleFileName(buf, 100);
    // And create a string out of it
    string str(buf);
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4

    Thread Starter
    Addicted Member Virtual24's Avatar
    Join Date
    May 2001
    Posts
    228
    Hmmmmm... That might help. I wanna know what is the best kind of variable to use tho, so i don't have to do all that... U can't write to a const char* variable or even a char* variable and so that seems kinda stupid that all those functions require a variable that is unstable and unwritable!!!???
    To protect time is to protect everything...

  5. #5
    Lively Member
    Join Date
    Oct 2001
    Posts
    80
    If you want the easy way out, use apstring class. Other wise I would create a pointer to char and work with that. And yes you can write and read using char * and it's very stable, but you will have to be using memory functions to make it dynamic. The type string and apstring both use char * as their base, but they took care of the memory manipulation for you.

  6. #6
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    you might be able to do

    Code:
    void ErrorMsg(string emsg)
    {
        MessageBox(NULL, puts(emsg), "", MB_OK );
    }
    Visit www.fragblast.com
    Gaming, forums, and a online RPG/Battle system




    (__Flagg) DOT NET? is this a Hindi Dating service?

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Code:
    void ErrorMsg(const string &emsg) {
        MessageBoxA(NULL, emsg.c_str(), "", MB_OK);
    }
    That should definitely work.

    Note the specific MessageBoxA - since string doesn't pay any attention to the Unicode settings, you need to specifically use wstring to get that. This just makes sure...
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    that's what my tstring is for:
    typedef basic_string<_TCHAR> tstring;

    Note that you can use c_str only for functions that require a const char* (LPCTSTR, PCTSTR, LPCSTR, PCSTR, LPWCSTR, PWCSTR, const TCHAR*, const wchar_t*, const WCHAR*, const _TCHAR*, ...)

    All parameters that lack the keyword const or the letter C in the acronym are non-const and need a real string buffer.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  9. #9
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    Originally posted by parksie
    Code:
    void ErrorMsg(const string &emsg) {
        MessageBoxA(NULL, emsg.c_str(), "", MB_OK);
    }
    Why would you pass it by reference?
    Alcohol & calculus don't mix.
    Never drink & derive.

  10. #10
    Zaei
    Guest
    I would say for performance issues (at minimum, an std::string must hold a char*, and a length, which makes for 6-8 bytes). Passing by reference speeds things along when passing parameters.

    Z.

  11. #11

    Thread Starter
    Addicted Member Virtual24's Avatar
    Join Date
    May 2001
    Posts
    228
    I believe i fixed the problem by simply putting:
    Code:
    void ErrorMsg( const char *msg, const char *caption )
    {
        MessageBox( msg, ........ so on
    }
    To protect time is to protect everything...

  12. #12
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    Just as an example here is what I use

    PHP Code:
    //Macro That Will Make A Error Message Box
    #define ERRMSG(X) MessageBox(NULL,X,"Error", MB_OK | MB_ICONSTOP) 
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  13. #13
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Ugh.

    Inline functions please, macros are evil
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  14. #14
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    I use Macros for easy things like this
    Inline for everything else.


    ....Figures you would be against Macros
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  15. #15
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Well, you can't put them in namespaces, and they knacker your code up

    As soon as I get a compiler (come on Ked, where the hell are ya? hehe ) I can continue with my Win32 API header file.

    If I don't decide to skip it and use .NET :s
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  16. #16
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    Yeah thats a good point about the namespaces

    Yeah when is Ked going to finish that. I want to check it out
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  17. #17
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Ked's busy with Squirrel at the moment.

    He said he might use my header since it does it all using templates and stuff rather than macros so everything follows the type checking rules.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  18. #18
    Zaei
    Guest
    Templates and inline functions are gooood =). Function style macros are bad =).

    Z.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width