Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to construct a CString from a char (mfc) ?

84 views
Skip to first unread message

davi...@hotmail.com

unread,
Jul 26, 1998, 3:00:00 AM7/26/98
to
I am new to mfc and I am having difficulty
creating a CString string from a char.

From the documentation one is supposed to be
able to create a string from
CString(const unsigned char* psz);

however, this seems to produce the following error:

void CScribbleView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
unsigned char ch = (unsigned char)(nChar) ;
const unsigned char* pch = &ch;
CString key = new CString(pch) ;
}


error C2664: 'CString::CString(const class CString &)'
: cannot convert parameter 1 from 'class CString *' to
'const class CString &' (new behavior; please see help)

Can anyone show me how to create a CString from a char

David
please reply also by email

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp Create Your Own Free Member Forum

Jac Goudsmit

unread,
Jul 26, 1998, 3:00:00 AM7/26/98
to
> CString key = new CString(pch) ;

You are constructing a CString from a CString* here (as the error
indicates): "new" returns a pointer to the instance (not the instance
itself). What you want is:

CString key(pch);

You could also use:

CString *key=new CString(pch);

The first example will destroy itself as soon as you leave the function. The
second example will be retained in memory and can be used to pass the value
to other functions or as return value. Do remember to destroy the second
version with "delete key;".

>please reply also by email

You're posting a problem on the newsgroup, read the newsgroup for the reply.

===Jac


Emerick Rogul

unread,
Jul 26, 1998, 3:00:00 AM7/26/98
to
david763 writes:

: I am new to mfc and I am having difficulty


: creating a CString string from a char.

: From the documentation one is supposed to be
: able to create a string from
: CString(const unsigned char* psz);

: however, this seems to produce the following error:

: void CScribbleView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
: {
: // TODO: Add your message handler code here and/or call default
: unsigned char ch = (unsigned char)(nChar) ;
: const unsigned char* pch = &ch;

: CString key = new CString(pch) ;
: }

: error C2664: 'CString::CString(const class CString &)'


: : cannot convert parameter 1 from 'class CString *' to
: 'const class CString &' (new behavior; please see help)

Well, you're attempting to assign a pointer value to a non-pointer
variable, which is a no-no. If you change the last line above to:
"CString* key = new CString(pch);" you should be OK (note the added
star). This is really a C++ issue, not an MFC one.

: Can anyone show me how to create a CString from a char

I believe it's just the assignment that's incorrect; once you fix
that, it should be fine.

-Emerick
--
-------------------------------------------------------------------------
"razzle, dazzle, drazzle, drone, time for this one to come home,
razzle, dazzle, drazzle, dry, time for this one to come alive."
--- Emerick Rogul <eme...@cs.bu.edu> ---------- 'hold my life', the mats

Jason Boehle

unread,
Jul 26, 1998, 3:00:00 AM7/26/98
to
> CString key = new CString(pch) ;

new CString(pch) returns a CString*, not just a CString.

Jason Boehle
jbo...@lawrence.ks.us

Shaun Wilde

unread,
Jul 27, 1998, 3:00:00 AM7/27/98
to
Missing the obvious:

1) CString(const unsigned char* psz);

is only good for NULL ('\0') terminated strings.

2) CString(TCHAR nChar)

this constructor will suffice

CString key((TCHAR)nChar) ;

or even

CString key;
key=(TCHAR)nChar);

because in non-unicode builds TCHAR == char

thus your code would be

void CScribbleView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default

CString key((TCHAR)nChar) ;

...
}

>
>From the documentation one is supposed to be
>able to create a string from
>CString(const unsigned char* psz);
>
>however, this seems to produce the following error:
>
>void CScribbleView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
>{
> // TODO: Add your message handler code here and/or call default
> unsigned char ch = (unsigned char)(nChar) ;
> const unsigned char* pch = &ch;

0 new messages