当我尝试做这样的事情时,我经常遇到这个错误
CString filePath = theApp->GetSystemPath() + "test.bmp";
编译器告诉我
error C2110: '+' : cannot add two pointers
但是如果我把它改成下面这样就可以了吗?
CString filePath = theApp->GetSystemPath();
filePath += "test.bmp";
函数 GetSystemPath
返回一个 LPCTSTR,如果这与它有任何关系的话
最佳答案
这与您正在处理的对象类型有关。
CString filePath = theApp->GetSystemPath() + "test.bmp";
上面的行试图用“test.bmp”或 LPCTSTR + char[] 添加 GetSystemPath() 的类型;编译器不知道如何执行此操作,因为这两种类型没有 + 运算符。
这样做的原因:
filePath += "test.bmp";
是因为你在做CString + char[](char*); CString 类重载了 + 运算符以支持添加 CString + char*。或者,在对两个 CString 对象应用加法运算符之前,从 char* 构造 CString。 LPCTSTR 没有重载此运算符或定义适当的构造函数。
关于C++ "cannot add two pointers",将硬编码字符串添加到 CString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16324599/