Casting Strings

D

Daniel Moree

I have a program that uses getline() to read a line from a file.
It's doing this for an INI file reader. I'm trying to get the line
parser function to remove the comments from each line if there are some.
I've got this done using: std::string. I need to cast it from
std::string(basic string) to a CString(LPCTSTR). When I use String.c_str
function, it won't cast it to an LPCTSTR, i've also tried a nonconstant
version: LPSTR.

Any help would be appreciated!
 
B

benben

Daniel Moree said:
I have a program that uses getline() to read a line from a file.
It's doing this for an INI file reader. I'm trying to get the line parser
function to remove the comments from each line if there are some. I've got
this done using: std::string. I need to cast it from std::string(basic
string) to a CString(LPCTSTR). When I use String.c_str function, it won't
cast it to an LPCTSTR, i've also tried a nonconstant version: LPSTR.

Any help would be appreciated!

Step 1, get the size of the std::string string (length() will do);
Step 2, allocate enough memory for the LPCTSTR. If LPCTSTR is a macro to a
const something than allocate for the non-const version;
Step 3, invoke std::copy, now you are done.

Ben
 
J

Jay Nabonne

I have a program that uses getline() to read a line from a file.
It's doing this for an INI file reader. I'm trying to get the line
parser function to remove the comments from each line if there are some.
I've got this done using: std::string. I need to cast it from
std::string(basic string) to a CString(LPCTSTR). When I use String.c_str
function, it won't cast it to an LPCTSTR, i've also tried a nonconstant
version: LPSTR.

Any help would be appreciated!

It's tricky to tell without seeing the actual error message (hint, hint),
but are you by chance doing a Unicode build? If so, you will have to
convert the string, since LPCSTSTR will point to wide characters.

- Jay
 
D

Daniel Moree

ok here's the error message:
c:\projects\codename_shadow\cconfig.cpp(46) : error C2664: 'fnError' :
cannot convert parameter 2 from 'const char *(void) const' to 'const char *'
There is no context in which this conversion is possible

here is the code:
bool CConfig::parseLine(std::string Line){
size_t offset = Line.find("//");
if(offset != std::string::npos){
Line = Line.substr(0, offset);
}
if(Line.length == 0){ return(true); }
fnError(hwnd, Line.c_str);

return(true);
}

void fnError(HWND hWnd, LPCTSTR msg){
ShowCursor(true);
MessageBox(hWnd, msg, "Error!", MB_OK | MB_ICONSTOP);
ShowCursor(false);
}
 
J

Jay Nabonne

ok here's the error message:
c:\projects\codename_shadow\cconfig.cpp(46) : error C2664: 'fnError' :
cannot convert parameter 2 from 'const char *(void) const' to 'const char *'
There is no context in which this conversion is possible

here is the code:
bool CConfig::parseLine(std::string Line){
size_t offset = Line.find("//");
if(offset != std::string::npos){
Line = Line.substr(0, offset);
}
if(Line.length == 0){ return(true); }
fnError(hwnd, Line.c_str);

c_str is a function.

fnError(hwnd, Line.c_str());

- Jay
 
K

Kai-Uwe Bux

Daniel said:
ok here's the error message:
c:\projects\codename_shadow\cconfig.cpp(46) : error C2664: 'fnError' :
cannot convert parameter 2 from 'const char *(void) const' to 'const char
*'
There is no context in which this conversion is possible

here is the code:
bool CConfig::parseLine(std::string Line){
size_t offset = Line.find("//");
if(offset != std::string::npos){
Line = Line.substr(0, offset);
}
if(Line.length == 0){ return(true); }
fnError(hwnd, Line.c_str);

Maybe:

fnError( hwnd, Line.c_str() );
return(true);
}

void fnError(HWND hWnd, LPCTSTR msg){
ShowCursor(true);
MessageBox(hWnd, msg, "Error!", MB_OK | MB_ICONSTOP);
ShowCursor(false);
}


Best

Kai-Uwe Bux
 
D

Daniel Moree

ok fixed, kept missing that c_str() function thing. Sometimes it helps
to have someone else look at the code.

Thanks again for everyones help!
 
P

Pete Becker

Daniel said:
ok fixed, kept missing that c_str() function thing. Sometimes it helps
to have someone else look at the code.

Now that you understand the problem, look again at the error message. It
really does point you there, but you have to know how to read it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,197
Latest member
Sean29G025

Latest Threads

Top