结构不接受 wchar_t

发布于 2024-09-30 19:05:11 字数 1304 浏览 6 评论 0原文

这是我唯一能想到的。这个东西是有感知的。

我有一个如下的结构:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

花心好男孩 2024-10-07 19:05:11

有两个主要问题。

首先,字符串文字 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 like L"blah blah" (note the L).

Second, const correctness: declare your formal argument like wchar_t const* pFirst. This allows using a literal directly as actual argument. Or any const string.

Cheers & hth.,

各空 2024-10-07 19:05:11

字符串必须以 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 type const wchar_t*. Without the L it's a narrow-character string, of type const char*, so the error is correct; attempting to cast a const char* to wchar_t* won't work, because you're just changing the type of the pointer and losing the const qualifier (it does nothing to the pointed-to data). Also, your second example is creating wchar_t objects from const char* pointers, which probably isn't what you want either - you want pointers, not just a single wchar_t object.

It's not sentient, you're just not clear what you're doing :)

零崎曲识 2024-10-07 19:05:11

尝试

dict_.push_back( NumPair ( L"anfangen", L"to begin, to start" ) ); 

L 表示它是一个 unicode (wchar) 字符串。

Try

dict_.push_back( NumPair ( L"anfangen", L"to begin, to start" ) ); 

The L denotes it's a unicode (wchar) string.

忆依然 2024-10-07 19:05:11

“引号中的字符串” 是一个 char 数组。如果不进行复制,则无法将其转换为 wchar_t 数组。但如果前面有一个 L,L"string inquotes" 就是文字,它会为您提供一个 wchar_t 数组。你希望 L 位于你的文字前面:

NumPair( L"anfangen", L"to begin, to start" )

A "string in quotes" is an array of chars. You can't convert that to an array of wchar_t without copying. But with an L in front, a L"string in quotes" is literal which gives you an array of wchar_ts instead. You want that L in front of your literals:

NumPair( L"anfangen", L"to begin, to start" )
嘦怹 2024-10-07 19:05:11

要创建 wchar_t 字符的字符串,请在文字上使用 L 前缀。

dict_.push_back( NumPair ( L"anfangen", L"to begin, to start" ) ); 

PS 如果您不创建比字符串长度大一的对象字符串来容纳零终止符,您就会遇到麻烦。您可以考虑使用std::wstring

To create strings of wchar_t characters, use the L prefix on the literals.

dict_.push_back( NumPair ( L"anfangen", L"to begin, to start" ) ); 

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.

花之痕靓丽 2024-10-07 19:05:11

最有可能的是,忘记像使用指针那样进行操作并使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文