C++ "cannot add two pointers",将硬编码字符串添加到 CString

标签 c++ string mfc

当我尝试做这样的事情时,我经常遇到这个错误

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/

相关文章:

c++ - reverse_iterators 和 base 令人费解的后递增和预递增行为

java - 如何在 stringbuilder 中放置空格

c++ - CWinThread 被 AfxBeginThread 创建后谁拥有它?

python - 如何在Python 3中使用for循环计算字符串内的单词数

C++正则表达式提取子字符串

c++ - 有没有更好的方法来选择正确的方法重载?

mfc - CEdit控件最大长度? (以可以显示的字符为单位)

c++ - 如何 memcpy() 一个常量

c++ - 断言 desc 在 src/libswscale/swscale_internal.h :668 失败

c++ - 光线追踪 : Sphere distortion due to Camera Movement