質問

I had the following code:

wchar_t recordsText[64] = L"Records: ";
std::wstringstream ss2;
ss2 << c;
wcsncat_s(recordsText, ss2.str().c_str(), sizeof(ss2.str().c_str()));
((CButton*)GetDlgItem(IDC_RECORDS))->SetWindowTextW(recordsText);

It worked pretty well, but i want to put it into a function... nothing easier i thought. but i get an stupid error.

my function was this one:

BOOL refreshTextField(CButton* item, wchar_t* label, long long* number){
    std::wstringstream ss;
    ss << number; 
    wcsncat_s(label, ss.str().c_str(), sizeof(ss.str().c_str()));
    item->SetWindowTextW(label);
    return true;
}

but the wcsncat_s doesnt like my "label" because its an array and the function is called like this:

refreshTextField(((CButton*)GetDlgItem(IDC_SENT_PACKAGES)), L"Packages send:  ", &sentPackages);

(btw: i know it shouldn't be casted to a CButton because it's an edit-field :-D , but that doesnt matter at the moment.)

the problem is the wchar_t array, i dont know how to get it into my function correctly. hope you can give me a quit answer.

i already tried this:

BOOL refreshTextField(CButton* item, wchar_t** label, long long* number){
    //...
    wcsncat_s(*label, sizeof(*label), ss.str().c_str(), sizeof(ss.str().c_str()));
    //....
}

and this:

BOOL refreshTextField(CButton* item, wchar_t* label, long long* number){
    //...
wcsncat_s(label, sizeof(*label), ss.str().c_str(), sizeof(ss.str().c_str()));
    //....
}

EDIT:

So the solution was this:

call:

refreshTextField(mySelectedUIItem, L"testlabel", sizeof(L"testlabel"), 4);

function:

BOOL refreshTextField(CButton* item, wchar_t* label, size_t lableSize, long long* number)
{
    std::wstringstream ss;
    ss << number;
    wcsncat_s(label, labelSize, ss.str().c_str(), ss.str().length());
    //...
}
役に立ちましたか?

解決

{Edit}

When you want to use the function template, you must match all parameter types. So you must pass the length of the string instead of a second copy of the c_str() result to the wcsncat_s template:

wcsncat_s(recordsText, ss2.str().c_str(), ss2.str().length());

This will resolve to the prototype

template <size_t size>
errno_t _mbsncat_s(
   unsigned char (&strDest)[size],
   const unsigned char *strSource,
   size_t count
); // C++ only

{/Edit}

Without the template the following applies:

You can't pass an array to a function. The function will only accept the pointer. The array can be well accessed with the pointer inside the function. But you lose the information about the array size.

Since the pointer only points to the first element of the array you can't use

sizeof(*somePointer);

because this gives you the size of the first array element.

You need to change the parameter list of refreshTextField. Since the label argument points to an output variable you need the size of the variable as an additional parameter. e.g.:

BOOL refreshTextField(CButton* item, wchar_t* label, size_t lableSize, long long* number)
{
    std::wstringstream ss;
    ss << number;
    wcsncat_s(label, labelSize, ss.str().c_str(), ss.str().length());
    //...
}

他のヒント

sizeof(ss2.str().c_str())

The result of function c_str() is wchar_t*. sizeof( wchar_t* ) is 4 or 8 bytes (on 32 or 64 bit system resp.). You should use wstring::length() function instead:

wcsncat_s( label, ss.str().c_str(), ss.str().length() );

try with this

BOOL refreshTextField(CButton* item, wchar_t[] label, long long* number){
    //...
   wcsncat_s(label, ss.str().c_str(), sizeof(ss.str().c_str()));
    //....
}

http://www.cplusplus.com/faq/sequences/arrays/sizeof-array/

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top