简体   繁体   中英

How can I correct this error in mfc?

I am using mfc which I compiled in Visual Studio 2010, and I am getting the following error:

error C2664: 'memmove' : cannot convert parameter 1 from 'CString' to 'void *'

My code is:

CString testArray[5];
for (int i = 0; i < 5; i++)
{
    testArray[i].Format("%d", i*4);
}

memmove(testArray[2], testArray[3], sizeof(testArray)-2*sizeof(testArray[0]));

for (int i = 0; i < 5; i++)
{
    MessageBox(testArray[i]);
}

How can I correct this error in mfc?

This is not error in the MFC. Error is in your code, which is completely invalid. Lets look at it carefully:

memmove(testArray[2], testArray[3], sizeof(testArray)-2*sizeof(testArray[0]));

  1. Using low-level memory copy routines like memmove with the complex data types is a sure proof way to failure. A class like CString may use a dynamical memory allocation and directly overriding its memory could cause any kind of trouble, beginning from the memory leakage and ending with immediate application failure. If you want to copy CString data use its own methods: CString::Insert , CString::Replace , assignment operator or copy constructor.

  2. memmove function takes two pointers to the source and destination memory blocks. You are trying to pass two CString instances instead. Of course this is invalid and never would pass a compiler type check. This is an example of the correct memmove usage.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM