2010-11-24 9 views
2

私はロケールファセットを使用して文字列にwstringのを変換しようとしているが保護されていますが、私は次のようなエラーに捕まってしまった:'仮想char型のstd :: CTYPE <wchar_t> :: do_narrow(wchar_tは、CHAR)constは' の

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; 
} 

誰かが私が間違っていますdooing、何を教えてもらえますか?

ありがとう

答えて

0

2物事:

1)use_facet戻りCONSTを参照するので、あなたは非constいずれかに割り当てることはできません。ようにCTを宣言する:2のエラーメッセージの状態として

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

2)、do_narrowは、外部の発信者に、それがアクセス不能なって、保護されています。代わってnarrowを使用してください。

+0

答えてくれてありがとう、どういうわけか私はctypeのすべての公開メンバーを見逃して、保護されたものだけを見ました。 – Trakhan

0

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

関連する問題