开发者

visual c++ operator += is ambiguous

CString szMsg;

//Other non related code

//stOrderInfo.bstrOrderNum is defi开发者_高级运维ned 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?


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:

szMsg += (CString)(", Order: ") + (CString)((char *)(stOrderInfo.bstrOrderNum));

Cast between string types:

How to: Convert Between Various String Types


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


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 like operator+ 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


Better use CString::AppendFormat. But ensure you pass correct format-specifier.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜