|
-
Mar 16th, 2002, 11:53 PM
#1
Thread Starter
Addicted Member
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...
-
Mar 17th, 2002, 09:58 AM
#2
Lively Member
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.
-
Mar 17th, 2002, 11:16 AM
#3
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.
-
Mar 17th, 2002, 01:25 PM
#4
Thread Starter
Addicted Member
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...
-
Mar 17th, 2002, 02:21 PM
#5
Lively Member
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.
-
Mar 18th, 2002, 06:41 AM
#6
Fanatic Member
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?
-
Mar 18th, 2002, 08:09 AM
#7
Monday Morning Lunatic
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
-
Mar 18th, 2002, 01:47 PM
#8
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.
-
Mar 18th, 2002, 10:13 PM
#9
Fanatic Member
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.
-
Mar 18th, 2002, 10:26 PM
#10
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.
-
Mar 19th, 2002, 12:04 AM
#11
Thread Starter
Addicted Member
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...
-
Mar 19th, 2002, 12:22 PM
#12
Frenzied Member
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

-
Mar 19th, 2002, 12:32 PM
#13
Monday Morning Lunatic
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
-
Mar 19th, 2002, 12:38 PM
#14
Frenzied Member
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

-
Mar 19th, 2002, 12:43 PM
#15
Monday Morning Lunatic
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
-
Mar 19th, 2002, 12:45 PM
#16
Frenzied Member
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

-
Mar 19th, 2002, 12:46 PM
#17
Monday Morning Lunatic
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
-
Mar 19th, 2002, 07:29 PM
#18
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|