C++"无法添加两个指针",将一个硬编码字符串添加到CString

2 c++ string mfc

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

CString filePath = theApp->GetSystemPath() + "test.bmp";
Run Code Online (Sandbox Code Playgroud)

编译告诉我

error C2110: '+' : cannot add two pointers
Run Code Online (Sandbox Code Playgroud)

但是,如果我将其改为以下,它可以正常工作吗?

CString filePath = theApp->GetSystemPath();
filePath += "test.bmp";
Run Code Online (Sandbox Code Playgroud)

该函数GetSystemPath返回LPCTSTR,如果它与它有任何关系

Chr*_*sCM 5

这与您正在处理的对象类型有关.

CString filePath = theApp->GetSystemPath() + "test.bmp";
Run Code Online (Sandbox Code Playgroud)

上面的一行是尝试使用"test.bmp"或LPCTSTR + char []添加GetSystemPath()的类型; 编译器不知道如何执行此操作,因为它们对于这两种类型不是+运算符.

这有效的原因:

filePath += "test.bmp";
Run Code Online (Sandbox Code Playgroud)

是因为你正在做CString + char [](char*); CString类重载了+运算符以支持添加CString + char*.或者可选地,在将两个CString对象上的加法运算符应用之前,从char*构造CString.LPCTSTR没有重载此运算符或定义了正确的构造函数.