Class array problem

I have a class (stuff) with a m_name member.
I can:
stuff* pstuff;
pstuff = new stuff;
CString name = _T("Ralph");
pstuff.m_name = name;

I can't:
stuff* pstuff;
pstuff = new stuff[3];
CString name = _T("Ralph");
*(pstuff+0)->m_name = name;

I get an error C2440: '=': cannot convert from 'CString' to 'const WCHAR'
why have things changed and what would be a good solution?

-- 
Just Al
0
Al1 (451)
2/16/2008 6:44:03 PM
vc.mfc 33608 articles. 0 followers. Follow

6 Replies
639 Views

Similar Articles

[PageSpeed] 23

I meant to use pstuff->m_name = name;
in the I can: code
-- 
Just Al


"Al" wrote:

> I have a class (stuff) with a m_name member.
> I can:
> stuff* pstuff;
> pstuff = new stuff;
> CString name = _T("Ralph");
> pstuff.m_name = name;
> 
> I can't:
> stuff* pstuff;
> pstuff = new stuff[3];
> CString name = _T("Ralph");
> *(pstuff+0)->m_name = name;
> 
> I get an error C2440: '=': cannot convert from 'CString' to 'const WCHAR'
> why have things changed and what would be a good solution?
> 
> -- 
> Just Al
0
Al1 (451)
2/16/2008 8:18:01 PM
On Sat, 16 Feb 2008 10:44:03 -0800, Al <Al@discussions.microsoft.com>
wrote:

>I have a class (stuff) with a m_name member.

What's the type of m_name? From the following, I guess you're talking
about:

struct stuff
{
   CString m_name;
};

However, this is also consistent with your code:

struct stuff
{
   LPCTSTR m_name; // Why would this be a problem?
};

>I can:
>stuff* pstuff;
>pstuff = new stuff;
>CString name = _T("Ralph");
>pstuff.m_name = name;

That cannot work. You need either:

   pstuff->m_name // Usual way
   (*pstuff).m_name // Rarely used

>I can't:
>stuff* pstuff;
>pstuff = new stuff[3];
>CString name = _T("Ralph");
>*(pstuff+0)->m_name = name;
>
>I get an error C2440: '=': cannot convert from 'CString' to 'const WCHAR'
>why have things changed and what would be a good solution?

You need to use the array syntax:

   pstuff[i].m_name

What you wrote reduces to:

   *pstuff->m_name
   *(pstuff->m_name)

If m_name really is a CString, your error message is due to the compiler
applying CString::operator LPCTSTR to the first element of the array and
then dereferencing the returned pointer. (I don't think CString implements
operator*; if it does, that's what the compiler is using, not operator
LPCTSTR.) If you really, really wanted to do things the longhand way, you
would need to say:

   (pstuff+i)->m_name
   (*(pstuff+i)).m_name

Of course, the array syntax already given is what you should be using:

   pstuff[i].m_name

All three of these are equivalent.

-- 
Doug Harrison
Visual C++ MVP
0
dsh (2498)
2/16/2008 8:29:01 PM
"Al" <Al@discussions.microsoft.com> ha scritto nel messaggio 
news:393688A7-FA1B-4077-9889-C6357C450381@microsoft.com...

> I can't:
> stuff* pstuff;
> pstuff = new stuff[3];
> CString name = _T("Ralph");
> *(pstuff+0)->m_name = name;

IMHO, you may also consider using robust C++ container classes like 
std::vector to store 'stuff' instances (or better pointers to them), e.g.

  std::vector< stuff > someStuff;

  // Add to array...
  someStuff.push_back(... );

or you can store pointers in the vector, like this:

  std::vector< stuff * > someStuff;

(if e.g. 'stuff' class instances are big).

You may also combine boost:: (or tr1) shared_ptr smart pointer with 
std::vector container, having a kind of garbage collector in C++.

Giovanni


0
2/16/2008 8:38:14 PM
That is certainly one of the weirdest excuses for syntax I've seen for assigning to an
array!  I wouldn't even TRY to figure out what it is doing.

Of course, you have made one of the obvious blunders in reporting a problem; you didn't
give any of the CRITICAL information, such as the type of m_name, which is, of course,
CString.  Isn't it?  (And if it isn't, WHY isn't it?  Of course, it is intuitively obvious
to everyone reading this exactly what your declaration is...)

Besides, you should not be declaring an array like this.  Use CArray<stuff> or
std::vector<stuff>.  Raw C arrays are just not best practice.

but the array access should have been
pstuff[0]->m_name = name;

and for that matter, you don't need the intermediate CString; you could have written

pstuff[0]->m_name = _T("Ralph");

without requiring a store to an intermediate variable at all.
						joe

On Sat, 16 Feb 2008 10:44:03 -0800, Al <Al@discussions.microsoft.com> wrote:

>I have a class (stuff) with a m_name member.
>I can:
>stuff* pstuff;
>pstuff = new stuff;
>CString name = _T("Ralph");
>pstuff.m_name = name;
>
>I can't:
>stuff* pstuff;
>pstuff = new stuff[3];
>CString name = _T("Ralph");
>*(pstuff+0)->m_name = name;
>
>I get an error C2440: '=': cannot convert from 'CString' to 'const WCHAR'
>why have things changed and what would be a good solution?
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
0
newcomer (15975)
2/16/2008 9:01:31 PM
Thanks to both of you very much.
-- 
Just Al


"Al" wrote:

> I meant to use pstuff->m_name = name;
> in the I can: code
> -- 
> Just Al
> 
> 
> "Al" wrote:
> 
> > I have a class (stuff) with a m_name member.
> > I can:
> > stuff* pstuff;
> > pstuff = new stuff;
> > CString name = _T("Ralph");
> > pstuff.m_name = name;
> > 
> > I can't:
> > stuff* pstuff;
> > pstuff = new stuff[3];
> > CString name = _T("Ralph");
> > *(pstuff+0)->m_name = name;
> > 
> > I get an error C2440: '=': cannot convert from 'CString' to 'const WCHAR'
> > why have things changed and what would be a good solution?
> > 
> > -- 
> > Just Al
0
Al1 (451)
2/16/2008 9:07:01 PM
I just read your thread and want to thank you for not just answering my 
question but directing me in the correct method to code this. And sorry about 
the critical information needed to analyze my question.

Thanks!
-- 
Just Al


"Joseph M. Newcomer" wrote:

> That is certainly one of the weirdest excuses for syntax I've seen for assigning to an
> array!  I wouldn't even TRY to figure out what it is doing.
> 
> Of course, you have made one of the obvious blunders in reporting a problem; you didn't
> give any of the CRITICAL information, such as the type of m_name, which is, of course,
> CString.  Isn't it?  (And if it isn't, WHY isn't it?  Of course, it is intuitively obvious
> to everyone reading this exactly what your declaration is...)
> 
> Besides, you should not be declaring an array like this.  Use CArray<stuff> or
> std::vector<stuff>.  Raw C arrays are just not best practice.
> 
> but the array access should have been
> pstuff[0]->m_name = name;
> 
> and for that matter, you don't need the intermediate CString; you could have written
> 
> pstuff[0]->m_name = _T("Ralph");
> 
> without requiring a store to an intermediate variable at all.
> 						joe
> 
> On Sat, 16 Feb 2008 10:44:03 -0800, Al <Al@discussions.microsoft.com> wrote:
> 
> >I have a class (stuff) with a m_name member.
> >I can:
> >stuff* pstuff;
> >pstuff = new stuff;
> >CString name = _T("Ralph");
> >pstuff.m_name = name;
> >
> >I can't:
> >stuff* pstuff;
> >pstuff = new stuff[3];
> >CString name = _T("Ralph");
> >*(pstuff+0)->m_name = name;
> >
> >I get an error C2440: '=': cannot convert from 'CString' to 'const WCHAR'
> >why have things changed and what would be a good solution?
> Joseph M. Newcomer [MVP]
> email: newcomer@flounder.com
> Web: http://www.flounder.com
> MVP Tips: http://www.flounder.com/mvp_tips.htm
> 
0
Al1 (451)
2/19/2008 2:42:01 PM
Reply:

Similar Artilces:

Dynamic Array for data consolidation
Hi, After I made use the codes from the same thread, modified for my need and later I found out that I do not know how to change the codes below from hard code array to dynamic array to take any number of excel workbooks ( ie it will increase from time to time ) from a folder and later to run data consolidation Sub DataConsol() Application.DisplayAlerts = False Application.ScreenUpdating = False Application.Calculation = xlCalculationManual Const MAXBOOK As Long = 5 Dim i%, SheetArg$() Dim sPath1 As String ReDim SheetArg(1 To MAXBOOK) Dim sPath As String, sFile As Strin...

SetForegroundWindow problem in 2000/XP
Hi I have to bring my application foreground during some events. SetForegroundWindow() function works fine in NT but had some problem in XP. I read in MSDN about SetForegroundWindow() restriction in 2000/XP. But when i tried the following code it seems to work fine in NT and XP . i have not tested it in Win 2000 void BringMyWindowForeground() { // get a pointer to our main window CWnd *pMainWnd = AfxGetMainWnd(); HWND forground_hwnd = ::GetForegroundWindow(); HWND PrevHndl = pMainWnd->m_hWnd; if(PrevHndl == NULL) return; DWORD lForeThreadID = 0; DWORD lThis...

[Money2007] Cleared or Reconcilied and other font problem
Hello, what does they mean? What's the difference? Another question: in my Working budget I see strange characters (like squaes in the values of column % of income. Does anybody has this problem? Thanks In microsoft.public.money, Pippo wrote: >Hello, >what does they mean? > >What's the difference? Cleared means that the bank has that transaction recognize, such as a check having cleared. Reconciled means that you have checked that the cleared transactions add up to what the bank thinks they have added up to. Those were not "official" but just my rough des...

Excel 2000 formula recalc problem
Hi everybody, I have a problem making sure all the formulas in the spreadsheet recalculate, when recalc is called from within a macro. I have a complicated spreadsheet model that takes a second or two to recalculate. I run a macro that: 1. Sets up parameters 2. Forces recalc 3. Grabs the output 4. Cycles back to step 1 with different parameters, etc. My code looks something like this: For j = pageFirst To pageLast Step pageStep <set up parameters of the j-th scenario> Application.CalculateFull <save the output, which is just a rect...

xml + xslt convert to rtf problem
Hi there, I create an XML file from a dataset like this: System.IO.StreamWriter xmlSW = new System.IO.StreamWriter(FILENAME); dsUserData1.WriteXml(xmlSW, XmlWriteMode.WriteSchema); xmlSW.Close(); Which gives me this XML file: <NewDataSet> <tblUserData> <User_ID>Administrator</User_ID> <User_Name>Some Name</User_Name> <User_Position>Analyst Programmer</User_Position> <User_Department>Forensic & Special</User_Department> <User_Phone>03 9632 0872</User_Phone> <User_Fax>03 9632 0875</U...

win2003 & exchange 2003 mail problem
Hi I just had some problems with my server, so my disks are all destroyed. So, I`ve installed win 2003 server enterprise and exchange 2003. Everything goes smooth but i cant get the mail to work. Even when i go into AD and add a user, i cant get the folder where i shall insert the mail specs either. It must be something i`ve forgot. Also when i go into system manager and rightclick on the upper line, and chose internet mail wizard, that is not working either. Could someone please help me? Or if someone knows of any good sites\guides to install exch. 2003 properly. thanks regards Asle Pett...

Report problems when upgrading
I have not upgraded from Money 98 because of a continual problem I have had with reports in each newer Money version. When I auto convert, or manually enter transactions, all transactions show up on the Performance by Investment Account report as being in the current year, no matter what the date is. For example, say I enter a purchase of $25,000 on 1/1/2000. If I look at a Performance by Investment Account for 1/1/2003 - 12/31/2003, I will see $25,000 in the "Buys" column. If if I click on it, there will be no transactions for the date range (because it was in 2000). ...

Scrolling problem with Intellimouse #2
I am using a Microsoft IntelliMouse Explorer 2.0 on a Dell computer with Win XP with SP2. Excel is 2002 w/ SP3. Intellipoint is v. 5.20.413.0 (6/3/2004). I just updated driver with no improvement. Unlike in other MS programs on this computer (including Word and IE), I am unable to use the mouse wheel to scroll up and down in an Excel file (spreadsheet). When I use the wheel in Excel, the Zoom changes, even if I click on the spreadsheet itself first. I want to fix this so that when I use the wheel, I am moving up and down in the spreadsheet. (I also use a similar IntelliMouse with ...

Problems with mail enabled security groups
Exchange 2003 EE SP1 Windows 2003 SP1 Hi at All, we have the following problems sending emails to mail enabled security groups. For example we have one security group like v_abt_de_g_w wich is mail enabled. The member of this group is an other security group with some user accounts. After sending an email to this mail enabled group the email dosn't reach its target. On Exchange Message Tracking Center you can see the following Entries : SMTP : Message submitteed to advanced queing SMTP: Starting message submission to advanced queue SMTP: Message submitted to categorizer But when ...

Tab delimited text problems within Excel
When opening a tab delimited file within excel I am experiencing problems. The file's last column is blank, which is needed, but when I open it within excel it recognises that the column exists for the first 15 rows then it doesn't recognises the column when saving back as a text file.. Any ideas? A lot of programs don't need the tab if there is nothing in the values. But if your program does, how about putting something in that last column that you want. Just put a single apostrophe (') in that last column of each row and excel will understand what you want. Andrew...

Partition Problem
Got a wierd one for ya. Everything was going along as normal and then all of a sudden, while out at lunch, our exchange server stopped working. 12 hours later I got it running after several chkdsk, defrag, repeats based on the error messages. The information store is safe and sound and so is the log store. But the drive with the OS on is freaking me out. Under disk management, the graphic display shows 21.77GB of HD space in the C: drive partition. Up top in the list view it shows 12.00GB. dbl-click my computer and C: drive is 12.00GB. What has happened to my partition? ...

ExMerge security problem
I'm trying to exmerge from my 5.5 machine to 2003. It has no problems creating the PST file but gets the following error: Error opening message store (MSEMS). Verify that the Microsoft Exchange Information Store service is running and that you have the correct permissions to log on. (0x8004011d) I found the knowledge base articles 322312 (http://support.microsoft.com/default.aspx?kbid=322312 ) abd 823143 about adjusting the security but it still doesn't work. Any other suggestions? -Rob T. ...

Migrating Product Problem
Hello everyone... now i want migrating Product data to CDF database (cdf_product).... but i notice that DefaultUoMScheduleId is require field must be fill in.. but i found that on UoMSchedule table dont have data... is that problem for migrating data?...or everybody got solution to solve this problem.. thanks... Go to Settings|Products|Unit Group Create unit of measure schedule. Each/Dozen/Gross etc. This can now be your default unit group. Additionally you will need to create a price list. -- --------------------------- Pierre Hulsebus - CRM Consultant http://www.ehtc.c...

Physical Inventory Problem?
I try to do physical inventory for all items in store but after I input quantity counted then click Calculate the message pop up said run-time error. Do you ever have the same issue? Can you tell what cause the problem? Thank you very much for help. ...

MFC message language problems
Hi! I have come across a really annoying problem. I am a norwegian programmer, but I make international software. This means that the text in my applications are English. However, some messages, like the message box asking if a modified document should be saved, is in Norwegian! Anyone experienced this, and have a solution? The application in question is a MFC single-document based. Oops, sorry, I was being stupid... :-) "Morten Aune Lyrstad" <morten@creventive.com> skrev i melding news:bBfPb.30980$BD3.6795283@juliett.dax.net... > Hi! I have come across a really annoying...

Copy Problems with VB
Not sure what is going on but I cannot figure it out. I have a worksheet where I will enter in a large amount of numbers. After one macro runs that will sort them, I want to be able to manually select and copy those numbers I want and paste them into another worksheet in the same workbook. I am able to copy and paste the numbers in the same sheet fine but when I copy and go to the other sheet, I am not able to paste. Then when I go back to the original sheet with the numbers, the "marching ants" the surround my selection is no longer there. The only time I can copy th...

Void Deferral Transactions Problem
I am having a problem voiding some deferred transactions. I entered the transactions through Receivables Management. I can see these transactions under the customers in the Deferral Inquiry window. However, they do not show up in the Void Deferral Transactions window for me to void. Some other transactions entered the same way in the same batch as these but under different customers do show up. Cannot figure out why that would be. I have tried check links and deferral check links to no avail. Any suggestions? Thanks! Paul ...

Worksheet protection problem #2
Hi all, I have the following worksheet protection problem in Excel 2003. Replicaton of the problem: 1. In a worksheet, unlock the range A2:F6 (in fact any rectangular range will do). 2. in Tools | Protection | Protect Sheets: a. Protect worksheets and contents of locked cells b. Check the "select locked cells) check box. c. allow all users of worksheet to insert and delete rows. 3. Now highlight the range A3:F3. 4. Select Insert | Row. Note that you can allow to insert rows. Q: How can I delete rows?? Thanks for your advice in advance. Frederick Chow Hong Kong. ...

Automatic Chart Expansion Problem
For years, I've be charting temperature variations. As I added new data points, the chart automatically expanded to accomdate the new datum. At 72 data points, the chart stopped expanding automatically. Anyone know if this is a standard limit to expanding charts? Did I accidently turn some feature off? Is there a way to fix this? I'm running MS Win and Office XP with all updates. Thanks Mike Hi, It really depends on how your expansion was working. Is your chart set to a fixed data range with 72 rows or columns that you have now filled up? If so you just need to extend the ra...

Problems downloading transactions from TD Ameritrade
I am getting ETF transaction downloads fine from TD Ameritrade, but not mutual fund transactions. I get the account balance update saying that, of course, my Money Plus Deluxe balance doesn't match that of the brokerage (because it didn't get the transactions.) I thought the problem might be that I was running Money 2006, so I updated to the 2008 Money Plus Deluxe but the problem remains. TD Ameritrade tech support just shrugs their shoulders. Has anyone else had this problem with TD Ameritrade? with any other broker? What was the solution? I've tried a lot the suggestio...

Problems Sending Mail #2
I am using Outlook 2003 with my employer's Exchange 2007 servers. As per their instructions, Outlook is set up to connect to the Exchange server via http. It is in cached mode. I'm seeing two main issues, and I wonder if they are connected and of course if they can be fixed. 1. I get frequent (more frequently than once an hour) messages that the connection to the Exchange server has been lost and then restored. The restored message always follows very quickly on the lost message; in fact, looking at the event viewer the restored message often seems to come just one second after t...

Yet, another TIME problem
Okay, I will attempt to explain my situation without just confusin anyone who reads this. I have a time sheet. It consists of the following fields: Time Started/Lunch Begin/Lunch End/Time Ended/Total Time/Over Time In the first four fields the cells are number format (looks lik military time but in reality it isn't) ie: 0630/1130/1230/1600 In the fifth field, the total time is a decimal value. It always read 8.00 or less and carries the remainder,if any, to the over time fiel which is also decimal value. The overtime has to be reported in quarte hour increments ie: .15, .25, .50, .75....

Split() order into array
Hi and TIA, I have a requirement where the Split() function must enter the elements into an array in the order of the string being split. For example, Dim sString As String, vArr As Variant sString = "zero;one;two;three" vArr = Split(sString, ";") therefore the following should be true: vArr(0) = "zero" vArr(1) = "one" vArr(2) = "two" vArr(3) = "three" In my tests this seems to always work. Can anyone confirm that Split() will ALWAYS order these correctly, before I go counting on it? VBA Help does not spec...

location problem: display bitmap
Hi, I create a CForm window (MFC) with a button (some other controls) on it. I need to display a bitmap (from a bitmap file) at the right side of the button. My problem is I cannot get the correct location. Below is my code. I believe I set the different view, which makes the pixel calculation not work. "GetSystemMetrics()" and "GetWindowRect()" may not work in here, but which function should work? thanks a lot larry ---------------------------------------------------------------------------- -- void ConView::Init_LogoBmp() { CScrollView::OnInitialUpdate(); CSize si...

Problem with Online Services - Citigroup Accounts Only
My Online Services for Transaction Data Download were working properly for approximately 1 yr. July 15th was my last successful connection and download. I now get the following error: "There is a problem with the data received from this online provider. If the problem persists, contact the providers customer service for help". I called Citigroup and they said that it is a money problem - they were useless. I am running Money 2000 and haven't changed a thing. Did the service end? Does the latest 'Update Internet Info'have an error for Citigroup? In microsoft.p...