2013-07-04 64 views
3

備註:string_algo工作得很好std::wstring,當然我可以(並且)將CString對象轉換爲std :: wstring first if當我需要一個來自string_algo的算法。這將是非常好的,但如果我可以放入CString對象 - 與現有代碼的集成將會更容易!如何使用MFC CString與提升字符串算法庫

我想做什麼:

CString in = ...; 
const CString out = boost::replace_last_copy(in, L"SEARCH", L"REPLACE"); 

由於suggested here我想還包括boost/range/mfc.hppBoost.Range adapted to MFC。 (雖然我不完全得到this header因爲看它似乎並不爲CString的做任何事情,只爲集合類。)

升壓1.44.0與Visual Studio 2005

測試代碼如下像這樣:

#include "stdafx.h" 
#include <iostream> 
#include <boost/range/mfc.hpp> 
#include <boost/algorithm/string.hpp> 

void f(CString const& input) { 
    CString out = boost::replace_last_copy(input, L"FROM", L"TO"); 
    std::wcout << out << "\n"; 
} 

我得到看起來像這樣的錯誤:

1>c:\programme\boost_library-1_44_0\boost\algorithm\string\detail\find_format_store.hpp(77) : error C2039: 'type' : is not a member of 'boost::range_const_iterator<C>' 
1>  with 
1>  [ 
1>   C=const CString 
1>  ] 
1>  c:\programme\boost_library-1_44_0\boost\algorithm\string\detail\find_format.hpp(139) : see reference to function template instantiation 'bool boost::algorithm::detail::check_find_result<const InputT,const FindResultT>(InputT &,FindResultT &)' being compiled 
1>  with 
1>  [ 
1>   InputT=CString, 
1>   FindResultT=boost::iterator_range<const wchar_t *> 
1>  ] 
1>  c:\programme\boost_library-1_44_0\boost\algorithm\string\find_format.hpp(113) : see reference to function template instantiation 'InputT boost::algorithm::detail::find_format_copy_impl<SequenceT,FormatterT,boost::iterator_range<IteratorT>>(const InputT &,FormatterT,const FindResultT &)' being compiled 
1>  with 
1>  [ 
1>   InputT=CString, 
1>   SequenceT=CString, 
1>   FormatterT=boost::algorithm::detail::const_formatF<boost::iterator_range<const wchar_t *>>, 
1>   IteratorT=const wchar_t *, 
1>   FindResultT=boost::iterator_range<const wchar_t *> 
1>  ] 
1>  c:\programme\boost_library-1_44_0\boost\algorithm\string\replace.hpp(314) : see reference to function template instantiation 'SequenceT boost::algorithm::find_format_copy<SequenceT,boost::algorithm::detail::last_finderF<SearchIteratorT,PredicateT>,boost::algorithm::detail::const_formatF<RangeT>>(const SequenceT &,FinderT,FormatterT)' being compiled 
1>  with 
1>  [ 
1>   SequenceT=CString, 
1>   SearchIteratorT=const wchar_t *, 
1>   PredicateT=boost::algorithm::is_equal, 
1>   RangeT=boost::iterator_range<const wchar_t *>, 
1>   FinderT=boost::algorithm::detail::last_finderF<const wchar_t *,boost::algorithm::is_equal>, 
1>   FormatterT=boost::algorithm::detail::const_formatF<boost::iterator_range<const wchar_t *>> 
1>  ] 
1>  ....\mytest.cpp(10) : see reference to function template instantiation 'SequenceT boost::algorithm::replace_last_copy<CString,const wchar_t[5],const wchar_t[3]>(const SequenceT &,Range1T (&),Range2T (&))' being compiled 
1>  with 
1>  [ 
1>   SequenceT=CString, 
1>   Range1T=const wchar_t [5], 
1>   Range2T=const wchar_t [3] 
1>  ] 
1>c:\programme\boost_library-1_44_0\boost\algorithm\string\detail\find_format_store.hpp(78) : error C3203: 'type' : unspecialized class template can't be used as a template argument for template parameter 'IteratorT', expected a real type 
1>c:\programme\boost_library-1_44_0\boost\algorithm\string\detail\find_format.hpp(121) : error C2780: 'void boost::algorithm::detail::insert(InputT &,InputT::iterator,const InsertT &)' : expects 3 arguments - 4 provided 
1>  c:\programme\boost_library-1_44_0\boost\algorithm\string\detail\sequence.hpp(39) : see declaration of 'boost::algorithm::detail::insert' 
1>  c:\programme\boost_library-1_44_0\boost\algorithm\string\detail\find_format.hpp(144) : see reference to function template instantiation 'InputT boost::algorithm::detail::find_format_copy_impl2<InputT,FormatterT,FindResultT,boost::iterator_range<IteratorT>>(const InputT &,FormatterT,const FindResultT &,const FormatResultT &)' being compiled 
1>  with 
1>  [ 
1>   InputT=CString, 
1>   FormatterT=boost::algorithm::detail::const_formatF<boost::iterator_range<const wchar_t *>>, 
1>   FindResultT=boost::iterator_range<const wchar_t *>, 
1>   IteratorT=const wchar_t *, 
1>   FormatResultT=boost::iterator_range<const wchar_t *> 
1>  ] 
1>c:\programme\boost_library-1_44_0\boost\algorithm\string\detail\find_format.hpp(121) : error C2893: Failed to specialize function template 'void boost::algorithm::detail::insert(InputT &,InputT::iterator,ForwardIteratorT,ForwardIteratorT)' 
1>  With the following template arguments: 
1>  'CString' 
1>  'const wchar_t *' 
1>c:\programme\boost_library-1_44_0\boost\algorithm\string\detail\find_format.hpp(123) : error C2893: Failed to specialize function template 'void boost::algorithm::detail::insert(InputT &,InputT::iterator,const InsertT &)' 
1>  With the following template arguments: 
1>  'CString' 
1>  'boost::iterator_range<IteratorT>' 
1>  with 
1>  [ 
1>   IteratorT=const wchar_t * 
1>  ] 
1>c:\programme\boost_library-1_44_0\boost\algorithm\string\detail\find_format.hpp(123) : error C2780: 'void boost::algorithm::detail::insert(InputT &,InputT::iterator,ForwardIteratorT,ForwardIteratorT)' : expects 4 arguments - 3 provided 
1>  c:\programme\boost_library-1_44_0\boost\algorithm\string\detail\sequence.hpp(29) : see declaration of 'boost::algorithm::detail::insert' 
1>c:\programme\boost_library-1_44_0\boost\algorithm\string\detail\find_format.hpp(125) : error C2780: 'void boost::algorithm::detail::insert(InputT &,InputT::iterator,const InsertT &)' : expects 3 arguments - 4 provided 
1>  c:\programme\boost_library-1_44_0\boost\algorithm\string\detail\sequence.hpp(39) : see declaration of 'boost::algorithm::detail::insert' 
1>c:\programme\boost_library-1_44_0\boost\algorithm\string\detail\find_format.hpp(125) : error C2893: Failed to specialize function template 'void boost::algorithm::detail::insert(InputT &,InputT::iterator,ForwardIteratorT,ForwardIteratorT)' 
1>  With the following template arguments: 
1>  'CString' 
1>  'const wchar_t *' 

回答

0

嘗試用CStringW更換CString,在C映射到CStringA

確切的錯誤代碼,標題中的哪一行呢?你可以嘗試在本地代碼中替換相同的錯誤嗎?

+0

這是一個非常明確的UNICODE構建,所以'CStringW'。完整的測試代碼存在問題。 (以一個大部分爲空的'main'爲模) –

+0

如果你向LPCWSTR聲明類型轉換,或者使用GetString,或者傳遞一個非常量CString對象,該怎麼辦? – Ajay