我正在使用在 Visual Studio 2010 中编译的 mfc,但出现以下错误:
error C2664: 'memmove' : cannot convert parameter 1 from 'CString' to 'void *'
我的代码是:
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]);
}
如何在 mfc 中更正此错误?
最佳答案
这不是 MFC 中的错误。错误在您的代码中,这是完全无效的。 让我们仔细看看:
memmove(testArray[2], testArray[3], sizeof(testArray)-2*sizeof(testArray[0]));
对复杂数据类型使用
memmove
等低级内存复制例程是确保失败的可靠方法。像CString
这样的类可能会使用动态内存分配,直接覆盖其内存可能会导致任何类型的问题,从内存泄漏开始到立即应用程序失败结束。如果您想复制CString
数据,请使用它自己的方法:CString::Insert
、CString::Replace
、赋值运算符或复制构造函数。memmove
函数采用两个指向源和目标内存块的指针。您正在尝试传递两个CString
实例。当然这是无效的,永远不会通过编译器类型检查。这是 example正确的memmove
用法。
关于c++ - 如何在 mfc 中更正此错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37872545/