-
December 29th, 2013, 03:44 PM
#1
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.
-
December 29th, 2013, 04:39 PM
#2
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)
-
December 29th, 2013, 04:45 PM
#3
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
-
December 29th, 2013, 05:04 PM
#4
Re: Unknown 3rd party code, need help and explanation
If you look at the boost source code and the line referenced, it is
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)
-
December 31st, 2013, 06:03 AM
#5
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
-
December 31st, 2013, 08:24 AM
#6
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
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)
-
December 31st, 2013, 09:00 AM
#7
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.
-
December 31st, 2013, 10:42 AM
#8
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)
-
December 31st, 2013, 08:36 PM
#9
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
-
January 12th, 2014, 08:29 AM
#10
Re: Unknown 3rd party code, need help and explanation
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
|