结构不接受 wchar_t
这是我唯一能想到的。这个东西是有感知的。
我有一个如下的结构:
struct NumPair
{
wchar_t *pFirst, *pSecond;
int count;
with ctor,复制赋值和构造 as
NumPair( wchar_t *pfirst, wchar_t *psecond, int count = 0)
NumPair( const NumPair& np )
NumPair& operator=( const NumPair& np )
这是我上一个问题的扩展,其中我要求一种方法对字符指针列表进行排序,其中包含特殊(德语)字符,例如 ü、ä、ö
。
该解决方案似乎使用宽字符类型,但编译器由于某种原因抛出了一百多个转换错误。
示例输入:
// dict_ is a container of NumPairs.
dict_.push_back( NumPair ( "anfangen", "to begin, to start" ) );
编译器抱怨它无法将 const char *
转换为 wchar_t
。很好,我将 Push_back 更改为
dict_.push_back( NumPair ( wchar_t("anfangen"), wchar_t("to begin, to start") ) );
编译器错误:找不到接受所有参数的 NumPair ctor。
什么。这。地狱。我尝试了完全重建,认为我的 VSC++10 搞砸了。不,我猜不会。
我做错了什么?
代码
构造函数、赋值和复制构造都是 wchar_t 指针的深层复制,如下所示。
wchar.h 已包含在内。
NumPair( wchar_t *pfirst, wchar_t *psecond, int count = 0)
: count(count)
{
size_t s1, s2;
s1 = wcslen(pfirst);
s2 = wcslen(psecond);
pFirst = new wchar_t[s1];
pSecond = new wchar_t[s2];
wcscpy(pFirst, pfirst);
wcscpy(pSecond, psecond);
}
That's the only thing I can think of. The thing is sentient.
I have a struct as follows:
struct NumPair
{
wchar_t *pFirst, *pSecond;
int count;
with ctor, copy assignment and construction as
NumPair( wchar_t *pfirst, wchar_t *psecond, int count = 0)
NumPair( const NumPair& np )
NumPair& operator=( const NumPair& np )
This is an extension of my last problem in which I was asking for a way to sort a list of characters pointers with them containing special (german) characters such as ü, ä, ö
.
The solution seems to be using wide character types, but the compiler is throwing over a hundred errors of conversion for some reason.
Sample input:
// dict_ is a container of NumPairs.
dict_.push_back( NumPair ( "anfangen", "to begin, to start" ) );
The compiler is complaining that it cannot convert a const char *
to a wchar_t
. Fine enough, I change the push_back to say
dict_.push_back( NumPair ( wchar_t("anfangen"), wchar_t("to begin, to start") ) );
Compiler error: Cannot find a NumPair ctor, that accepts all arguments.
What. The. Hell. I tried a full rebuild, thinking my VSC++10 is mucking up. Nope, guess not.
What am I doing wrong?
CODE
The ctor, assignment and copy construction are all deep copies of the wchar_t pointers like below.
wchar.h is included.
NumPair( wchar_t *pfirst, wchar_t *psecond, int count = 0)
: count(count)
{
size_t s1, s2;
s1 = wcslen(pfirst);
s2 = wcslen(psecond);
pFirst = new wchar_t[s1];
pSecond = new wchar_t[s2];
wcscpy(pFirst, pfirst);
wcscpy(pSecond, psecond);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
有两个主要问题。
首先,字符串文字
wchar_t
的写法类似于L"blah blah"
(注意L
)。其次,
const
正确性:声明您的形式参数,如wchar_t const* pFirst
。这允许直接使用文字作为实际参数。或任何const
字符串。干杯&呵呵,
There are two main issues.
First, a string literal of
wchar_t
is written likeL"blah blah"
(note theL
).Second,
const
correctness: declare your formal argument likewchar_t const* pFirst
. This allows using a literal directly as actual argument. Or anyconst
string.Cheers & hth.,
字符串必须以 L 开头才能成为宽字符字符串文字,例如
L"abcdefghijk"
,其类型为const wchar_t*
。如果没有 L,它是一个窄字符串,类型为 const char*,因此错误是正确的;尝试将const char*
转换为wchar_t*
是行不通的,因为您只是更改了指针的类型并丢失了const
> 限定符(它对指向的数据没有任何作用)。另外,您的第二个示例是从 const char* 指针创建 wchar_t 对象,这可能也不是您想要的 - 您需要指针,而不仅仅是单个wchar_t
对象。它没有感知能力,你只是不清楚自己在做什么:)
Strings have to begin with an L to be wide-character string literals, e.g.
L"abcdefghijk"
, which is of typeconst wchar_t*
. Without the L it's a narrow-character string, of typeconst char*
, so the error is correct; attempting to cast aconst char*
towchar_t*
won't work, because you're just changing the type of the pointer and losing theconst
qualifier (it does nothing to the pointed-to data). Also, your second example is creatingwchar_t
objects fromconst char*
pointers, which probably isn't what you want either - you want pointers, not just a singlewchar_t
object.It's not sentient, you're just not clear what you're doing :)
尝试
L 表示它是一个 unicode (wchar) 字符串。
Try
The L denotes it's a unicode (wchar) string.
“引号中的字符串”
是一个char
数组。如果不进行复制,则无法将其转换为wchar_t
数组。但如果前面有一个 L,L"string inquotes"
就是文字,它会为您提供一个wchar_t
数组。你希望 L 位于你的文字前面:A
"string in quotes"
is an array ofchar
s. You can't convert that to an array ofwchar_t
without copying. But with an L in front, aL"string in quotes"
is literal which gives you an array ofwchar_t
s instead. You want that L in front of your literals:要创建 wchar_t 字符的字符串,请在文字上使用
L
前缀。PS 如果您不创建比字符串长度大一的对象字符串来容纳零终止符,您就会遇到麻烦。您可以考虑使用
std::wstring
。To create strings of wchar_t characters, use the
L
prefix on the literals.P.S. If you don't create your object strings one larger than the string length to accomodate the zero terminator, you'll be in trouble. You might consider using
std::wstring
.最有可能的是,忘记像使用指针那样进行操作并使用 std::wstring,这是采用宽字符串的内置字符串类。
如果您总是要使用指向文字的指针,那么您应该使用 const wchar_t * 指针。
请注意,您的结构仍然可以分配,因为成员不是常量指针,它们是指向不可变数据的指针。
Most likely, just forget about doing it the way you are with pointers and use std::wstring, the built-in string class that takes wide character strings.
If you are always going to use pointers to literals then you should use const wchar_t * pointers.
Note that your struct will still be assignable as the members are not constant pointers, they are pointers to immutable data.