void Me :: you(){} int main(){我::你(); 返回0; } // 2 //在main()编译后定义你() // void Me :: you(){} /// :〜 //////////////////////////////////////////// ///////////////////////////// 如果我::你()是*定义*在( 2)在main()调用之后,gcc 4.1.1 说:错误:''你''不是''我''的成员。相反,如果Me ::你在*(1)定义*,那么它就会编译。 MSVC ++ Express可以正常编译它,没有问题,正如预期的那样。 这肯定是gcc中的一个bug? 如果你改成它会发生什么 int main(){ 我: :我们::你(); 返回0; 我会认为我们是是必要的。 嗯?你()不是我们的成员的功能。这是我们的朋友*。 -Howard Hello All,I am doing another exercise (I repeat, *exercise*). The (irrelevant tothisdiscussion) point is to show that "You can inject a friend declarationintoa namespace by declaring it within an enclosed class". I have donethissuccessfully, but please consider the following program://: C10:FriendInjection.cpp// From Thinking in C++, 2nd Edition// Available at http://www.BruceEckel.com// (c) Bruce Eckel 2000// Copyright notice in Copyright.txtnamespace Me {class Us {//...public:friend void you();};}// 1void Me::you(){}int main() {Me::you();return 0;}// 2// defining you() after main() does not compile// void Me::you(){}///:~/////////////////////////////////////////////////////////////////////////If Me::you() is *defined* at (2) after the call in main(), gcc 4.1.1says:"error: ''you'' is not a member of ''Me''". If, instead, Me::you is*defined* at (1),then it compiles.MSVC++ Express compiles it either way without a problem, as expected.Surely this is a bug in gcc? 解决方案<jo**********@hotmail.comwrote in messagenews:11*********************@q23g2000hsg.googlegro ups.com...Hello All,I am doing another exercise (I repeat, *exercise*). The (irrelevant tothisdiscussion) point is to show that "You can inject a friend declarationintoa namespace by declaring it within an enclosed class". I have donethissuccessfully, but please consider the following program://: C10:FriendInjection.cpp// From Thinking in C++, 2nd Edition// Available at http://www.BruceEckel.com// (c) Bruce Eckel 2000// Copyright notice in Copyright.txtnamespace Me { class Us { //... public: friend void you(); };}// 1void Me::you(){}int main() { Me::you(); return 0;}// 2// defining you() after main() does not compile// void Me::you(){}///:~/////////////////////////////////////////////////////////////////////////If Me::you() is *defined* at (2) after the call in main(), gcc 4.1.1says:"error: ''you'' is not a member of ''Me''". If, instead, Me::you is*defined* at (1),then it compiles.MSVC++ Express compiles it either way without a problem, as expected.Surely this is a bug in gcc?Why?Declaring a function as a friend does not declare or define that function.It merely states that said function is a friend.In case (1), you''re declaring *and* defining Me::you(). If you move that toafter main() as in case (2), then when main() is compiled, Me::you() has notyet been declaredand so it''s an error.-Howard On May 16, 11:38 am, [email protected] wrote:Hello All,I am doing another exercise (I repeat, *exercise*). The (irrelevant tothisdiscussion) point is to show that "You can inject a friend declarationintoa namespace by declaring it within an enclosed class". I have donethissuccessfully, but please consider the following program://: C10:FriendInjection.cpp// From Thinking in C++, 2nd Edition// Available athttp://www.BruceEckel.com// (c) Bruce Eckel 2000// Copyright notice in Copyright.txtnamespace Me { class Us { //... public: friend void you(); };}// 1void Me::you(){}int main() { Me::you(); return 0;}// 2// defining you() after main() does not compile// void Me::you(){}///:~/////////////////////////////////////////////////////////////////////////If Me::you() is *defined* at (2) after the call in main(), gcc 4.1.1says:"error: ''you'' is not a member of ''Me''". If, instead, Me::you is*defined* at (1),then it compiles.MSVC++ Express compiles it either way without a problem, as expected.Surely this is a bug in gcc?What happens if you change it toint main() {Me::Us::you();return 0;I would have thought that the "Us" would be necessary."JLS" <de********@yahoo.comwrote in messagenews:11**********************@n59g2000hsh.googlegr oups.com...On May 16, 11:38 am, [email protected] wrote:>Hello All,I am doing another exercise (I repeat, *exercise*). The (irrelevant tothisdiscussion) point is to show that "You can inject a friend declarationintoa namespace by declaring it within an enclosed class". I have donethissuccessfully, but please consider the following program://: C10:FriendInjection.cpp// From Thinking in C++, 2nd Edition// Available athttp://www.BruceEckel.com// (c) Bruce Eckel 2000// Copyright notice in Copyright.txtnamespace Me { class Us { //... public: friend void you(); };}// 1void Me::you(){}int main() { Me::you(); return 0;}// 2// defining you() after main() does not compile// void Me::you(){}///:~/////////////////////////////////////////////////////////////////////////If Me::you() is *defined* at (2) after the call in main(), gcc 4.1.1says:"error: ''you'' is not a member of ''Me''". If, instead, Me::you is*defined* at (1),then it compiles.MSVC++ Express compiles it either way without a problem, as expected.Surely this is a bug in gcc?What happens if you change it toint main() { Me::Us::you(); return 0;I would have thought that the "Us" would be necessary.Huh? The function you() is not a member of Us. It''s a *friend* of Us.-Howard 这篇关于函数在调用之前声明但在之后定义 - 编译失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-19 22:12