CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Mar 2009
    Posts
    48

    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

  2. #2
    Join Date
    Jul 2002
    Posts
    2,543

    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.

  3. #3
    Join Date
    Mar 2009
    Posts
    48

    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

  4. #4
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

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

  5. #5
    Join Date
    Mar 2009
    Posts
    48

    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

  6. #6
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    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.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  7. #7
    Join Date
    Mar 2009
    Posts
    48

    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

  8. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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
  •  





Click Here to Expand Forum to Full Width

Featured