|
-
October 19th, 2009, 03:25 AM
#1
Compiler change leads to err?
Code:
c_progvs ci_progvs(argc, argv);
char *p_s_filename_log = &ci_progvs.m_s_filename_log;
ofstream f_logfile(p_s_filename_log);
The above code worked a while back. I guess my compiler has been upgraded. Still a bit surprised to get:
error : cannot convert 'std::string*' to '*char' in initialisation.
Just would like to know is this a compiler difference? Am I mistaken and my code has been unintentionally changed ?
Fairly sure that previous compiler was MINGW. Now says it is a GNU compiler.
Any help appreciated as I have no technical support to help apart from you guys. Cheers
-
October 19th, 2009, 03:43 AM
#2
Re: Compiler change leads to err?
char *p_s_filename_log = (char*)ci_progvs.m_s_filename_log.c_str();
or:
char *p_s_filename_log = const_cast<char*>(ci_progvs.m_s_filename_log.c_str());
Newest compilers make strong type checking. Your existing code may not work, because it assignes to char* pointer to std::string, this doesn't make sence.
-
October 19th, 2009, 05:39 AM
#3
Re: Compiler change leads to err?
Tx Alex F
I'm afraid I'm making a meal of this. I do not remember having an issue with scope with this code before but .....
Code:
if ( y == 0 ) char *p_s_filename_script = const_cast<char*>(ci_progvs.m_s_filename_script1.c_str());
if ( y == 1 ) char *p_s_filename_script = const_cast<char*>(ci_progvs.m_s_filename_script2.c_str());
ifstream scriptfile( p_s_filename_script );
And I get an out of scope error. I tried pre-declaring the pointer but to no avail.
Any guidance very welcome cause I'm going round in circles here. Cheers Nigel
-
October 19th, 2009, 06:04 AM
#4
Re: Compiler change leads to err?
1) you should not cast away the const unless there are extreme circumstances.
2) No need to cast away const in this case:
Code:
const char * p_s_filename_script;
if (y == 0) p_s_filename_script = ci_progvs.m_s_filename_script1.c_str();
if (y == 1) p_s_filename_script = ci_progvs.m_s_filename_script2.c_str();
ifstream scriptfile( p_s_filename_script );
-
October 19th, 2009, 06:16 AM
#5
Re: Compiler change leads to err?
Philip many thanks. I'm now cooking with gas.
I've been looking around for articles on const and casting where I might find some examples. My fat book (Sams Teach Yourself C++) offers a light dusting but no details and examples.
I like using fat books (£30/$50 - 800 pages) if you know what I mean. I find it the simplest way to learn and find them good value. Can anyone suggest an advanced fat book I could use to move on with C++? I need to get to grips with the details. (Note I started 10 months ago and am loving this stuff).
Much appreciate input from you folks here of course. Tx Nigel
-
October 19th, 2009, 07:36 AM
#6
Re: Compiler change leads to err?
Try the 'Effective C++' series by Scott Meyers - I've not read it for a while but he does have some very good stuff in there.
They're not 'FAT' but they are full of content.
Darwen.
-
October 19th, 2009, 12:11 PM
#7
Re: Compiler change leads to err?
Tx Darwen they look the very thing. And I can get access to them online quite cheap to have a good look.
I note there are many 2005 but hopefully you guys here can help me with any recent changes
-
October 19th, 2009, 12:48 PM
#8
Re: Compiler change leads to err?
I've always thought it was a bit odd that the stream classes weren't overloaded to understand std::strings.....
Tags for this Thread
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
|