-
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???
-
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.
-
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);
-
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!!!???
-
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.
-
you might be able to do
Code:
void ErrorMsg(string emsg)
{
MessageBox(NULL, puts(emsg), "", MB_OK );
}
-
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...
-
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.
-
Quote:
Originally posted by parksie
Code:
void ErrorMsg(const string &emsg) {
MessageBoxA(NULL, emsg.c_str(), "", MB_OK);
}
Why would you pass it by reference?
-
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.
-
I believe i fixed the problem by simply putting:
Code:
void ErrorMsg( const char *msg, const char *caption )
{
MessageBox( msg, ........ so on
}
-
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)
-
Ugh.
Inline functions please, macros are evil ;)
-
I use Macros for easy things like this
Inline for everything else.
....Figures you would be against Macros ;)
-
Well, you can't put them in namespaces, and they knacker your code up :D
As soon as I get a compiler (come on Ked, where the hell are ya? hehe :p) I can continue with my Win32 API header file.
If I don't decide to skip it and use .NET :s
-
Yeah thats a good point about the namespaces
Yeah when is Ked going to finish that. I want to check it out
-
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.
-
Templates and inline functions are gooood =). Function style macros are bad =).
Z.