{"id":40,"date":"2013-04-11T11:21:29","date_gmt":"2013-04-11T03:21:29","guid":{"rendered":"http:\/\/www.yarpee.com\/?p=40"},"modified":"2013-04-11T11:21:29","modified_gmt":"2013-04-11T03:21:29","slug":"error-c2440-initializing-cannot-convert-from-pctstr-to-atlcstringt","status":"publish","type":"post","link":"http:\/\/www.yarpee.cn\/?p=40","title":{"rendered":"error C2440: &#8216;initializing&#8217; : cannot convert from &#8216;PCTSTR&#8217; to &#8216;ATL::CStringT&#8217;"},"content":{"rendered":"<p>CAtlString\u662f\u53ef\u4ee5\u5728ANSI\u548cUNICODE\u4e4b\u95f4\u8f6c\u6362\u7684\uff0c\u4f8b\u5982CAtlStringA strSomething = lpszSomething\uff0c\u8fd9\u91cc\u7684lpszSomething\u662fwchar_t\u5b57\u7b26\u4e32\u7c7b\u578b\u3002<\/p>\n<p>\u53ef\u662f\u4f60\u4f1a\u53d1\u73b0\u8fd9\u79cd\u8bed\u53e5\u6709\u65f6\u5019\u53ef\u4ee5\u7f16\u8fc7\uff0c\u6709\u65f6\u5019\u53c8\u4e0d\u53ef\u4ee5\u7f16\u8fc7\uff0c\u975e\u8981\u7528CAtlStringA strSomething(lpszSomething)\uff1a<br \/>\n<code>error C2440: 'initializing' : cannot convert from 'PCTSTR' to 'ATL::CStringT&lt;BaseType,StringTraits&gt;'<br \/>\nwith<br \/>\n[<br \/>\nBaseType=char,<br \/>\nStringTraits=ATL::StrTraitATL&lt;char,ATL::ChTraitsCRT&lt;char&gt;&gt;<br \/>\n]<br \/>\nConstructor for class 'ATL::CStringT&lt;BaseType,StringTraits&gt;' is declared 'explicit'<br \/>\nwith<br \/>\n[<br \/>\nBaseType=char,<br \/>\nStringTraits=ATL::StrTraitATL&lt;char,ATL::ChTraitsCRT&lt;char&gt;&gt;<br \/>\n]<\/code><\/p>\n<p>\u9047\u5230\u8fd9\u79cd\u95ee\u9898\u771f\u662f\u6291\u90c1\uff0c\u660e\u77e5\u662f2B\u7684\u95ee\u9898\u5374\u4e0d\u660e\u771f\u76f8\uff0c\u6bcf\u6b21\u9047\u5230\u53ea\u80fd\u9000\u7f29\u6539\u4e3a\u663e\u5f0f\u6784\u9020\u3002<\/p>\n<p>\u8fd9\u6b21\u6df1\u7a76\u4e86\u4e00\u4e0b\uff0cCAtlString\u7684\u5b9e\u73b0\u4f1a\u843d\u5728CStringT\u4e0a\uff0cCStringT\u7684\u6784\u9020\u51fd\u6570\u5bf9\u4e8eYCHAR\uff08\u5373\u53e6\u4e00\u79cd\u5b57\u7b26\u7c7b\u578b\uff0cCStringT\u628a\u81ea\u8eab\u7684\u5b57\u7b26\u7c7b\u578b\u5b9a\u4e49\u4e3aXCHAR\uff09\u4f5c\u53c2\u6570\u5982\u4e0b\uff1a<br \/>\n<code>CSTRING_EXPLICIT CStringT( __in_z_opt const YCHAR* pszSrc )<\/code><\/p>\n<p>\u6ce8\u610f\u8fd9\u91cc\u6709\u4e2aCSTRING_EXPLICIT\u5b8f\uff0c\u5b83\u88ab\u5b9a\u4e49\u4e3a\uff1a<br \/>\n<code>#ifdef _ATL_CSTRING_EXPLICIT_CONSTRUCTORS<br \/>\n#define CSTRING_EXPLICIT explicit<br \/>\n#else<br \/>\n#define CSTRING_EXPLICIT<br \/>\n#endif<\/code><\/p>\n<p>\u539f\u6765\u662f\u6709_ATL_CSTRING_EXPLICIT_CONSTRUCTORS\u5b8f\u5b9a\u4e49\u7684\u65f6\u5019\u4e0d\u540c\u7c7b\u578b\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u9700\u8981\u663e\u5f0f\u8c03\u7528\u6784\u9020\u51fd\u6570\uff0c\u770b\u770bstdafx.h\u91cc\u8fd8\u771f\u6709\u8fd9\u4e2a\u5b8f\u5b9a\u4e49\uff1a<br \/>\n<code>#define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS \/\/ some CString constructors will be explicit<\/code><\/p>\n<p>\u5220\u6389\u8fd9\u4e2a\u5b8f\u5b9a\u4e49\u5c31\u80fd\u4f7f\u7528CAtlStringA strSomething = lpszSomething\u8fd9\u7c7b\u7528\u6cd5\u4e86\u3002\u4e3a\u4ec0\u4e48\u9700\u8981\u8fd9\u4e48\u4e00\u4e2a\u5b8f\u53bb\u63a7\u5236\u8fd9\u7c7b\u6784\u9020\u51fd\u6570\u9700\u8981\u663e\u5f0f\u8c03\u7528\uff1f<\/p>\n<p>MS\u8bf4\u5f97\u5f88\u6e05\u695a\u4e86\uff0c\u9632\u6b62\u65e0\u5fc3\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u3002<a href=\"http:\/\/msdn.microsoft.com\/zh-cn\/library\/xwh16t15(v=VS.100).aspx\" target=\"_blank\">_ATL_CSTRING_EXPLICIT_CONSTRUCTORS<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>CAtlString\u662f\u53ef\u4ee5\u5728ANSI\u548cUNICODE\u4e4b\u95f4\u8f6c\u6362\u7684\uff0c\u4f8b\u5982CAtlStringA strSometh &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/www.yarpee.cn\/?p=40\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201cerror C2440: &#8216;initializing&#8217; : cannot convert from &#8216;PCTSTR&#8217; to &#8216;ATL::CStringT&#8217;\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-40","post","type-post","status-publish","format-standard","hentry","category-c_plus_plus"],"_links":{"self":[{"href":"http:\/\/www.yarpee.cn\/index.php?rest_route=\/wp\/v2\/posts\/40"}],"collection":[{"href":"http:\/\/www.yarpee.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.yarpee.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.yarpee.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.yarpee.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=40"}],"version-history":[{"count":0,"href":"http:\/\/www.yarpee.cn\/index.php?rest_route=\/wp\/v2\/posts\/40\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.yarpee.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=40"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yarpee.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=40"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yarpee.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=40"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}