CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Dec 2010
    Posts
    907

    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 03:46 PM.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

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

    What types are parent.add() expecting?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Dec 2010
    Posts
    907

    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
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Dec 2010
    Posts
    907

    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
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #7
    Join Date
    Dec 2010
    Posts
    907

    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 09:02 AM.

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  9. #9
    Join Date
    Dec 2010
    Posts
    907

    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
    Largo, FL.
    Posts
    356

    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
  •  





Click Here to Expand Forum to Full Width

Featured