视觉c++运算符 += 不明确
CString szMsg;
//Other non related code
//stOrderInfo.bstrOrderNum is defined as a _bstr_t
szMsg += ", Order: " + stOrderInfo.bstrOrderNum;
我将上述内容从 VS 6.0 转换为 VS2k10,并且收到以下错误(在 VS 6.0 中编译):
error C2593: 'operator +=' is ambiguous
这到底是什么意思以及如何修复它?
CString szMsg;
//Other non related code
//stOrderInfo.bstrOrderNum is defined as a _bstr_t
szMsg += ", Order: " + stOrderInfo.bstrOrderNum;
I'm converting the above from VS 6.0 to VS2k10 and I'm getting the following error (compiles in VS 6.0):
error C2593: 'operator +=' is ambiguous
What exactly does this mean and how can I fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
因为您已经硬编码了 ", Order: " 编译器很难决定它应该是什么类型。
明显的类型应该是
CString
,但它可能会尝试将其转换为其他字符串类型,并向其中添加数字。因此它可能无法确定它是 CString 还是其他字符串类型。因此它无法决定您要添加到 szMsg 中的类型。
您可以只使用类型转换:
在字符串类型之间转换:
如何:在各种字符串类型之间进行转换
Because you've hard-coded ", Order: " the compiler is having a hard time to decide which type it should be.
The obvious type should be
CString
, but it might try to make it to some other string type, and add the number to it.So it probably can't decide if it's a
CString
or another string type. So it can't decide what type you're adding to szMsg.You could just use a type cast:
Cast between string types:
How to: Convert Between Various String Types
这意味着编译器无法选择对 BSTR + 字符连接使用哪个 + 运算。存在三种类型不匹配:CString、_bstr_t 和 char。
尝试将所有三个操作数统一为单一类型,例如 CString
This means that compiler cannot choose which + operation to use for BSTR + char concatenations. You have a mismatch of three types: CString, _bstr_t, and char.
Try to unify all three operands to a single type, e.g. to CString
已知
CString::operator+=
的实现在 Visual Studio 2010 中发生了变化。例如,在以前的版本中,它可以正常处理嵌入的空字符,就像operator+
一直在做的那样,但新版本没有。所以可能和这个有关。编辑
有关此主题的讨论的链接:
http://social.msdn.microsoft.com/Forums/en-US/vcmfcatl/thread/c5d7f383-da80-4776-b9b8-a6065839bd87
The implementation of
CString::operator+=
is known to have changed in Visual Studio 2010. For example in previous versions it handled embedded null characters OK, just likeoperator+
keeps doing, but the new version doesn't. So it might be related to this.EDIT
Link to discussion on this topic:
http://social.msdn.microsoft.com/Forums/en-US/vcmfcatl/thread/c5d7f383-da80-4776-b9b8-a6065839bd87
最好使用
CString::AppendFormat
。但请确保您传递正确的格式说明符。Better use
CString::AppendFormat
. But ensure you pass correct format-specifier.