{"id":52,"date":"2007-10-13T15:24:41","date_gmt":"2007-10-13T19:24:41","guid":{"rendered":"http:\/\/www.prglab.com\/blog\/p\/52"},"modified":"2008-07-01T22:41:00","modified_gmt":"2008-07-02T02:41:00","slug":"operator-reload-bug-in-vc60","status":"publish","type":"post","link":"https:\/\/www.prglab.com\/blog\/p\/52","title":{"rendered":"VC6.0\u4e2d\u91cd\u8f7d\u64cd\u4f5c\u7b26\u51fd\u6570\u65e0\u6cd5\u8bbf\u95ee\u7c7b\u7684\u79c1\u6709\u6210\u5458"},"content":{"rendered":"<p>\u5728 C++ \u4e2d\uff0c\u64cd\u4f5c\u7b26(\u8fd0\u7b97\u7b26\uff09\u53ef\u4ee5\u88ab\u91cd\u8f7d\u4ee5\u6539\u5199\u5176\u5b9e\u9645\u64cd\u4f5c\u3002\u540c\u65f6\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u4e3a\u7c7b\u7684\u670b\u53cb\u51fd\u6570(friend function)\u4ee5\u4fbf\u4f7f\u5f97\u8fd9\u4e2a\u51fd\u6570\u80fd\u591f\u8bbf\u95ee\u7c7b\u7684\u79c1\u6709\u6210\u5458\uff0c\u8fd9\u4e2a\u5b9a\u4e49\u901a\u5e38\u5728\u5934\u6587\u4ef6\u4e2d\u5b8c\u6210\u3002<\/p>\n<p>\u5728Visual C++\u4e2d\u5b9a\u4e49\u4e00\u822c\u7684\u51fd\u6570\u4e3a\u670b\u53cb\u51fd\u6570\u901a\u5e38\u662f\u6ca1\u6709\u95ee\u9898\u7684\u3002\u7136\u800c\u5bf9\u67d0\u4e9b\u91cd\u8f7d\u64cd\u4f5c\u7b26\u7684\u51fd\u6570\uff0c\u5373\u4f7f\u6211\u4eec\u5c06\u5b83\u4eec\u5b9a\u4e49\u4e3a\u7c7b\u7684\u670b\u53cb\u51fd\u6570\uff0cVC\u7684\u7f16\u8bd1\u5668\u4ecd\u7136\u4f1a\u663e\u793a\u51fa\u9519\u4fe1\u606f\uff0c\u8ba4\u4e3a\u8fd9\u4e9b\u670b\u53cb\u51fd\u6570\u65e0\u6743\u8bbf\u95ee\u7c7b\u7684\u79c1\u6709\u6210\u5458\u3002\u6211\u8ba4\u4e3a\u8fd9\u5e94\u8be5\u662fVC6.0\u7684bug\u3002<!--more--><\/p>\n<p>\u4ee5\u4e0b\u4ee3\u7801\u5c31\u662f\u4e2a\u4f8b\u5b50:<\/p>\n<table style=\"border: 1px solid #aaaaaa\" border=\"0\">\n<tr>\n<td style=\"background-color: #eeeeee\">\n<pre><span style=\"color: green\">\/\/ \u5934\u6587\u4ef6 \"Sample.h\"<\/span>\r\n#include&lt;iostream&gt;\r\nusing namespace std;\r\n\r\nclass Sample {\r\npublic:\r\n\tSample();\r\n\tfriend ostream &amp;operator&lt;&lt;(ostream &amp;out, const Sample s);\r\n\tfriend istream &amp;operator&gt;&gt;(istream &amp;in, Sample &amp; s);\r\n\r\nprivate:\r\n\tint x;\r\n};<\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<pre><\/pre>\n<table style=\"border: 1px solid #aaaaaa\" border=\"0\" height=\"100\" width=\"464\">\n<tr>\n<td style=\"background-color: #eeeeee\">\n<pre><span style=\"color: green\">\/\/ \u5b9e\u73b0\u6587\u4ef6 \"Sample.cpp\"<\/span>\r\n<span style=\"color: green\"><\/span>#include \"Sample.h\"\r\n\r\nSample::Sample() {\r\n\tx=0;\r\n}\r\n\r\nistream &amp;operator&gt;&gt;(istream &amp;in, Sample &amp; s) {\r\n\tcout&lt;&lt;\"Please enter a value\"&lt;&lt;endl;\r\n\tin &gt;&gt; s.x ;\r\n\treturn in;\r\n}\r\n\r\nostream &amp;operator&lt;&lt;(ostream &amp;out, const Sample s) {\r\n\tcout &lt;&lt; s.x &lt;&lt; endl;\r\n\treturn out;\r\n}<\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u5728gnuc++\u4e2d\u7f16\u8bd1\u8fd0\u884c\u6beb\u65e0\u95ee\u9898\u3002\u4f46\u662f\u5728VC++6.0\u4e2d\u7f16\u8bd1\u7684\u65f6\u5019\u5c31\u4f1a\u51fa\u73b0\u4ee5\u4e0b\u7684\u7f16\u8bd1\u9519\u8bef\uff1a<\/p>\n<table style=\"border: 1px solid #aaaaaa\" border=\"0\">\n<tr>\n<td style=\"background-color: #eeeeee\">Compiling&#8230;<br \/>\nSample.cpp<br \/>\nc:\\temp\\sample.cpp(8) : error C2248: &#8216;x&#8217; : cannot access private member declared in class &#8216;Sample&#8217;<br \/>\nc:\\temp\\sample.h(19) : see declaration of &#8216;x&#8217;<br \/>\nc:\\temp\\sample.cpp(13) : error C2248: &#8216;x&#8217; : cannot access private member declared in class &#8216;Sample&#8217;<br \/>\nc:\\temp\\sample.h(19) : see declaration of &#8216;x&#8217;<br \/>\nError executing cl.exe.Sample.obj &#8211; 2 error(s), 0 warning(s)<\/td>\n<\/tr>\n<\/table>\n<p>\u5728VC++ 6.0\u4e2d\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u6709\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a<\/p>\n<ul type=\"1\">\n<li>\u5728\u5934\u6587\u4ef6\u4e2d\u5b9e\u73b0\u4f5c\u4e3a\u670b\u53cb\u51fd\u6570\u7684\u64cd\u4f5c\u7b26\u51fd\u6570\u7684\u91cd\u8f7d\uff0c\u4e5f\u5c31\u662f\u8bf4\u5728\u5b9e\u73b0\u6587\u4ef6&#8221;Sample.cpp&#8221;\u4e2d\u5c06\u51fd\u6570\u91cd\u8f7d\u7684\u5b9e\u73b0\u53bb\u6389\uff0c\u800c\u5c06\u5934\u6587\u4ef6\u4fee\u6539\u5982\u4e0b\uff1a<br \/>\n<table style=\"border: 1px solid #aaaaaa\" border=\"0\">\n<tr>\n<td style=\"background-color: #eeeeee\">\n<pre><span style=\"color: green\">\/\/ \u4fee\u6539\u540e\u7684\u5934\u6587\u4ef6 1 \"Sample.h\"<\/span>\r\n#include&lt;iostream&gt;\r\nusing namespace std;\r\n\r\nclass Sample {\r\npublic:\r\n\tSample();\r\n\tfriend ostream &amp;operator&lt;&lt;(ostream &amp;out, const Sample s);\r\n\tfriend ostream &amp;operator&lt;&lt;(ostream &amp;out, const Sample s) {\r\n\t    cout &lt;&lt; s.x &lt;&lt; endl;\r\n\t    return out;\r\n\t}\r\n\r\n\tfriend istream &amp;operator&gt;&gt;(istream &amp;in, Sample &amp; s);\r\n\tfriend istream &amp;operator&gt;&gt;(istream &amp;in, Sample &amp; s) {\r\n\t\tcout&lt;&lt;\"Please enter a value\"&lt;&lt;endl;\r\n\t    in &gt;&gt; s.x ;\r\n\t    return in;\r\n\t}\r\nprivate:\r\n\tint x;\r\n};<\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<pre><\/pre>\n<\/li>\n<li>\u5728\u5934\u6587\u4ef6\u4e2d\u7c7b\u5b9a\u4e49\u4e4b\u524d\u5c06\u7c7b\u548c\u670b\u53cb\u64cd\u4f5c\u7b26\u51fd\u6570\u7684\u539f\u578b\u7279\u522b\u58f0\u660e\u4e00\u4e0b\uff0c\u4e5f\u5c31\u662f\u5c06\u5934\u6587\u4ef6\u4fee\u6539\u5982\u4e0b(\u5b9e\u73b0\u6587\u4ef6&#8221;Sample.cpp&#8221;\u4e0d\u7528\u4f5c\u4efb\u4f55\u4fee\u6539)\uff1a<br \/>\n<table style=\"border: 1px solid #aaaaaa\" border=\"0\">\n<tr>\n<td style=\"background-color: #eeeeee\">\n<pre><span style=\"color: green\">\/\/ \u4fee\u6539\u540e\u7684\u5934\u6587\u4ef6 2 \"Sample.h\"<\/span>\r\n#include&lt;iostream&gt;\r\nusing namespace std;\r\n\r\n<span style=\"color: green\">\/\/ \u4ee5\u4e0b3\u884c\u4ee3\u7801\u4e3a\u65b0\u52a0\u5165<\/span>\r\nclass Sample;\r\nostream &amp;operator&lt;&lt;(ostream &amp;out, const Sample s);\r\nistream &amp;operator&gt;&gt;(istream &amp;in, Sample &amp; s);\r\n\r\nclass Sample {\r\npublic:\r\n\tSample();\r\n\tfriend ostream &amp;operator&lt;&lt;(ostream &amp;out, const Sample s);\r\n\tfriend istream &amp;operator&gt;&gt;(istream &amp;in, Sample &amp; s);\r\n\r\nprivate:\r\n\tint x;\r\n};<\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<pre><\/pre>\n<\/li>\n<li>\u7b2c\u4e09\u79cd\u65b9\u6cd5\u662f\u5bf9I\/O\u540d\u7a7a\u95f4\u7684\u4f7f\u7528\u5b9e\u884c\u660e\u786e\u58f0\u660e\uff0c\u4e5f\u5c31\u662f\u8bf4\u5728\u5934\u6587\u4ef6&#8221;Sample.h&#8221;\u4e2d\u76f4\u63a5\u5199\uff1a<br \/>\n#include&lt;iostream&gt;<br \/>\nusing std::ostream;<br \/>\nusing std::istream<br \/>\n&#8230;.<br \/>\n\u53d6\u4ee3 &#8220;using namespace std;&#8221;<br \/>\n<strong> \u6ce8\u610f\uff1a<\/strong>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u91cc\u6211\u4eec\u5728\u5b9e\u73b0\u6587\u4ef6 &#8220;Sample.cpp&#8221;\u4e2d\u5305\u542b &#8220;using namespace std;&#8221;\u8fd9\u53e5\u8bdd\uff0c\u5426\u5219\u5728\u5b9e\u73b0\u4e2d\u5c31\u4e0d\u80fd\u4f7f\u7528 &#8220;cout&#8221; , &#8220;cin&#8221;, &#8220;&lt;&lt; &#8220;, &#8220;&gt;&gt;&#8221; \u548c endl \u8fd9\u4e9b\u5173\u952e\u5b57\u548c\u7b26\u53f7\u3002\u4fee\u6539\u540e\u7684\u5b8c\u6574\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<table style=\"border: 1px solid #aaaaaa\" border=\"0\">\n<tr>\n<td style=\"background-color: #eeeeee\">\n<pre><span style=\"color: green\">\/\/ Sample.h<\/span>\r\n#include&lt;iostream&gt;\r\n\r\nusing std::istream;\r\nusing std::ostream;\r\n\r\nclass Sample {\r\npublic:\r\n\tSample();\r\n\tfriend ostream &amp;operator&lt;&lt;(ostream &amp;out, const Sample s);\r\n\t\/*friend ostream &amp;operator&lt;&lt;(ostream &amp;out, const Sample s) {\r\n\t    cout &lt;&lt; s.x &lt;&lt; endl;\r\n\t    return out;\r\n\t}*\/\r\n    friend istream &amp;operator&gt;&gt;(istream &amp;in, Sample &amp; s);\r\n\t\/*friend istream &amp;operator&gt;&gt;(istream &amp;in, Sample &amp; s) {\r\n\t\tcout&lt;&lt;\"Please enter a value\"&lt;&lt;endl;\r\n\t    in &gt;&gt; s.x ;\r\n\t    return in;\r\n\t}*\/\r\nprivate:\r\n\tint x;\r\n};<\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<table style=\"border: 1px solid #aaaaaa\" border=\"0\" height=\"265\" width=\"486\">\n<tr>\n<td style=\"background-color: #eeeeee\">\n<pre><span style=\"color: green\">\/\/ \"Sample.cpp\"<\/span>\r\n#include \"Sample.h\"\r\nusing namespace std;\r\n\r\nSample::Sample() {\r\n\tx=5;\r\n}\r\nistream &amp;operator&gt;&gt;(istream &amp;in, Sample &amp; s) {\r\n\t\tcout&lt;&lt;\"Please enter a value\"&lt;&lt;endl;\r\n\t    in &gt;&gt; s.x ;\r\n\t    return in;\r\n\t}\r\n\r\nostream &amp;operator&lt;&lt;(ostream &amp;out, const Sample s) {\r\n\tcout &lt;&lt; s.x &lt;&lt; endl;\r\n\treturn out;\r\n}<\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<pre><\/pre>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 C++ \u4e2d\uff0c\u64cd\u4f5c\u7b26(\u8fd0\u7b97\u7b26\uff09\u53ef\u4ee5\u88ab\u91cd\u8f7d\u4ee5\u6539\u5199\u5176\u5b9e\u9645\u64cd\u4f5c\u3002\u540c\u65f6\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u4e3a\u7c7b\u7684\u670b\u53cb\u51fd\u6570(friend function)\u4ee5\u4fbf\u4f7f\u5f97\u8fd9\u4e2a\u51fd\u6570\u80fd\u591f\u8bbf\u95ee\u7c7b\u7684\u79c1\u6709\u6210\u5458\uff0c\u8fd9\u4e2a\u5b9a\u4e49\u901a\u5e38\u5728\u5934\u6587\u4ef6\u4e2d\u5b8c\u6210\u3002 \u5728Visual C++\u4e2d\u5b9a\u4e49\u4e00\u822c\u7684\u51fd\u6570\u4e3a\u670b\u53cb\u51fd\u6570\u901a\u5e38\u662f\u6ca1\u6709\u95ee\u9898\u7684\u3002\u7136\u800c\u5bf9\u67d0\u4e9b\u91cd\u8f7d\u64cd\u4f5c\u7b26\u7684\u51fd\u6570\uff0c\u5373\u4f7f\u6211\u4eec\u5c06\u5b83\u4eec\u5b9a\u4e49\u4e3a\u7c7b\u7684\u670b\u53cb\u51fd\u6570\uff0cVC\u7684\u7f16\u8bd1\u5668\u4ecd\u7136\u4f1a\u663e\u793a\u51fa\u9519\u4fe1\u606f\uff0c\u8ba4\u4e3a\u8fd9\u4e9b\u670b\u53cb\u51fd\u6570\u65e0\u6743\u8bbf\u95ee\u7c7b\u7684\u79c1\u6709\u6210\u5458\u3002\u6211\u8ba4\u4e3a\u8fd9\u5e94\u8be5\u662fVC6.0\u7684bug\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8],"tags":[],"_links":{"self":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/posts\/52"}],"collection":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/comments?post=52"}],"version-history":[{"count":0,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/posts\/52\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/media?parent=52"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/categories?post=52"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/tags?post=52"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}