C ++“无法添加两个指针",将硬编码的字符串添加到CString [英] C++ "cannot add two pointers", adding a hardcoded string to a CString

查看:329
本文介绍了C ++“无法添加两个指针",将硬编码的字符串添加到CString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I get this error quite often when I try to do something like this

CString filePath = theApp->GetSystemPath() + "test.bmp";

编译器告诉我

error C2110: '+' : cannot add two pointers

但是,如果我将其更改为以下内容,则可以正常工作吗?

But if I change it to this below it works fine?

CString filePath = theApp->GetSystemPath();
filePath += "test.bmp";

函数GetSystemPath返回与其相关的LPCTSTR

The function GetSystemPath returns a LPCTSTR if that has anything to do with it

推荐答案

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

This has to do with the types of objects that you are dealing with.

CString filePath = theApp->GetSystemPath() + "test.bmp";

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

The line above is attempting to add the type of GetSystemPath() with "test.bmp" or an LPCTSTR + char[]; The compiler does not know how to do this because their is no + operator for these two types.

此方法起作用的原因:

filePath += "test.bmp";

是因为您正在执行CString + char [](char *); CString类的+运算符已重载以支持添加CString + char *.或者,这是在将加法运算符应用于两个CString对象之前,从char *构造CString的方法. LPCTSTR没有重载此运算符或未定义适当的构造函数.

Is because you are doing CString + char[] (char*); The CString class has the + operator overloaded to support adding CString + char*. Or alternatively which is constructing a CString from a char* prior to applying the addition operator on two CString objects. LPCTSTR does not have this operator overloaded or the proper constructors defined.

这篇关于C ++“无法添加两个指针",将硬编码的字符串添加到CString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆