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
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.
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
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 );
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
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.
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
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.....