2

ロケールファセットを使用して wstring を文字列に変換しようとしていますが、次のエラーで行き詰まりました:

test_facet.cpp: In function ‘int main()’:
test_facet.cpp:14: error: invalid initialization of reference of type ‘std::ctype<wchar_t>&’ from expression of type ‘const std::ctype<wchar_t>’
/usr/include/c++/4.4/bits/locale_facets.h:1430: error: ‘virtual char std::ctype<wchar_t>::do_narrow(wchar_t, char) const’ is protected
test_facet.cpp:16: error: within this context

ソース:

#include <iostream>
#include <string>
#include <locale>
#include <algorithm>

using namespace std;

int main()
{
 locale loc("");
 std::wstring Str = L"ěščřžýáíé";
 std::string Str2;
 ctype<wchar_t> &ct = std::use_facet<std::ctype<wchar_t> >(loc);
 for(std::wstring::const_iterator It = Str.begin(); It < Str.end(); ++It)
   ct.do_narrow(*It, 'X' );
 std::cout << Str2 <<std::endl;
}

誰かが私に何を間違っているのか教えてもらえますか?

ありがとう

4

2 に答える 2

0

このコンテキストから do_narrow を呼び出すことはできません。クラス ctype (および派生) のメンバー メソッドのみが do_narrow を呼び出すことができます。

于 2010-11-24T20:35:55.500 に答える
0

2つのこと:

1) use_facetconst への参照を返すため、非 const に割り当てることはできません。ct を次のように宣言します。

 const ctype<wchar_t> &ct = ....

2) 2 番目のエラー メッセージに示されているように、do_narrowは保護されているため、外部の呼び出し元はアクセスできません。narrow代わりに public を使用してください。

于 2010-11-24T20:44:28.560 に答える