Unknown 3rd party code, need help and explanation
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: Unknown 3rd party code, need help and explanation

  1. #1
    Join Date
    Dec 2010
    Posts
    593

    Unknown 3rd party code, need help and explanation

    Hello,
    I am using boost property_tree ptree here
    Code:
    class _cochar {
    private:
      BOOL m_bAutoDelete;
      LPSTR m_szBuffer;
    
    public:
      _cochar(LPCWSTR wszText, BOOL bAutoDelete = TRUE)
      {
        m_bAutoDelete = bAutoDelete;
        _ASSERTE(wszText);
        int nLen = wcslen(wszText)+1;
        m_szBuffer = (LPSTR)::CoTaskMemAlloc(nLen * sizeof(CHAR));
        wcstombs(m_szBuffer, wszText, nLen);
      }
      
      ~_cochar()
      {
        if (m_bAutoDelete)
          ::CoTaskMemFree(m_szBuffer);
      }
      
      operator LPCSTR()
      {
        return (LPCSTR)m_szBuffer;
      }
    };
    Code:
    ptree& frame = parent.add("FrameName", _cochar(n.c_str()));

    In the second coding, I don't understand the error
    Code:
    Error	2	error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'const _cochar' (or there is no acceptable conversion)	e:\programming\libraries\boost_1_54_0\boost\property_tree\stream_translator.hpp	33	1	SimExporter2
    When I cancel the _cochar(), just n.c_str() in the second snippet, it works
    Please help!
    Thanks
    Jack
    Last edited by lucky6969b; December 29th, 2013 at 02:46 PM.

  2. #2
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,328

    Re: Unknown 3rd party code, need help and explanation

    What types are parent.add() expecting?
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    Dec 2010
    Posts
    593

    Re: Unknown 3rd party code, need help and explanation

    Code:
    template<class Type>
            self_type &add(const path_type &path, const Type &value);
    I believe it is expecting a template class type

  4. #4
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,328

    Re: Unknown 3rd party code, need help and explanation

    If you look at the boost source code and the line referenced, it is
    Code:
    s << e;
    where e is effectively of type const Type &value. Hence, for the .add method to work, there must be an operator<< defined for the type used. In this case, there needs to be an operator<< defined for the _cochar class.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  5. #5
    Join Date
    Dec 2010
    Posts
    593

    Re: Unknown 3rd party code, need help and explanation

    Sorry 2kaud, at first blush, I seldom use operator overriding, could you give me an example on this?
    Happy new year 2014
    Thanks
    Jack

  6. #6
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,328

    Re: Unknown 3rd party code, need help and explanation

    As you have defined the LPCSTR operator cast, it might be easier to use
    Code:
    ptree& frame = parent.add("FrameName", (LPCSTR)_cochar(n.c_str()));
    as the boost code has
    Code:
    s << e
    where s is a templated class of basic_ostream.

    and a Happy New Year to you.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  7. #7
    Join Date
    Dec 2010
    Posts
    593

    Re: Unknown 3rd party code, need help and explanation

    Hi
    It really works, thanks
    But what is the principle behind? Why casting would work with s << e where s is a template class?
    Does it bypass the operator method?
    I'd like to know.
    Thanks
    Jack
    Last edited by lucky6969b; December 31st, 2013 at 08:02 AM.

  8. #8
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,328

    Re: Unknown 3rd party code, need help and explanation

    s << e has been defined for e of type LPCSTR. So in your parent.add(), as long as the second param is of type LPCSTR then the .add class function is fine. As your _cochar class defined the operator LPCSTR, then (LPCSTR)_cochar(n.cstr()) results in a LPCSTR type which is fine. Sometimes an operator cast is easier to implement than providing an alternative operator overloading function.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  9. #9
    Join Date
    Dec 2010
    Posts
    593

    Re: Unknown 3rd party code, need help and explanation

    Oh I see, so it's the last method LPCSTR() does the magic, Thanks so much
    Jack

  10. #10
    Join Date
    Jan 2013
    Location
    Dallas, TX
    Posts
    129

    Re: Unknown 3rd party code, need help and explanation

    Testing.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center