C++ ‘;虚拟字符std::ctype<;wchar_t>;::do#u狭窄(wchar,char)常数’;受到保护

C++ ‘;虚拟字符std::ctype<;wchar_t>;::do#u狭窄(wchar,char)常数’;受到保护,c++,locale,facets,C++,Locale,Facets,我试图使用区域设置方面将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

我试图使用区域设置方面将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
test_facet.cpp:在函数“int main()”中:
test_facet.cpp:14:错误:从“const std::ctype”类型的表达式初始化“std::ctype&”类型的引用无效
/usr/include/c++/4.4/bits/locale_facets.h:1430:错误:“virtual char std::ctype::do_狭窄(wchar\u t,char)const”受保护
test_facet.cpp:16:错误:在此上下文中
资料来源:

#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;
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
地点loc(“”);
std::wstring Str=L“ěšššššššššššš;
std::字符串Str2;
ctype&ct=std::使用切面(loc);
对于(std::wstring::const_迭代器It=Str.begin();Itstd::cout您不能从此上下文调用do_狭窄。只允许类ctype(和deriveds)的成员方法调用do_狭窄。

您不能从此上下文调用do_狭窄。只允许类ctype(和deriveds)的成员方法调用do_狭窄。

两件事:

1)
use\u facet
返回对常量的引用,因此不能将其分配给非常量引用。因此将ct声明为:

 const ctype<wchar_t> &ct = ....
constctype&ct=。。。。
2) 正如第二条错误消息所述,
do\u-shown
受到保护,使外部呼叫者无法访问。请改用公开的
shown

2件事:

1)
use\u facet
返回对常量的引用,因此不能将其分配给非常量引用。因此将ct声明为:

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

2) 正如第二条错误消息所述,
do\u-shown
受到保护,使外部呼叫者无法访问。请改用
shown
,这是公开的。

感谢您的回答,不知何故,我错过了ctype的所有公开成员,只查看了受保护的成员:(愚蠢的美赞克为了回答这个问题,不知何故,我错过了所有ctype的公众成员,只看了受保护的成员:(愚蠢的我