本文介绍了模板朋友功能注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一个关于在模板中注入朋友功能的问题 类。我的问题是特定于gcc(版本3.4.5)在 与mingw的组合中使用,因为这段代码(或至少代码得到 相同的结果)按预期工作在visualc ++中。我知道这是 可能不是编译器的正确行为,但它是我正在寻找的那种 行为所以我希望有在gcc中使用 相同的方法。 正如您所知,当您定义一个类(没有模板)并且在 内时你定义了一个友元函数的类,该函数直接在类本身外部注入 范围。 ---代码--- void函数(); int main(int argc,char * argv []) { 函数(); //这个有效 } 等级测试 { 公开: friend void函数(){printf(" Function()");的getchar(); } }; - 结束代码 - 现在我想模仿模板类的相同行为,但是 这段代码不起作用: - 代码 - void Function() ; int main(int argc,char * argv []) { 函数(); //这不起作用 } 模板< typename T> 类测试 { public: friend void Function(){printf(" Function()");的getchar(); } }; 模板类测试< int> ;; - 结束代码 - 具体来说,我在链接时收到一个关于Function()的错误,而不是定义,我猜这是因为编译器不考虑 函数的定义是一个非模板函数而是 假设它是一个模板函数,因为它是在模板类的里面定义的。 现在,有趣的是,如果你移动main()以下的模板类Test的显式 实例化,这段代码实际编译了 并且有效,这就是为什么我认为它是一个错误的原因。因为 理论上如果Function()的定义是模板函数 (因此不是正常函数)它永远不应该用于调用 $ b main()函数内部的$ b,无论main()函数是否低于实例化的b $ b。我是对的吗? 无论如何我正在寻找一种方法来定义函数() 可用于main()函数,即使main()也是如此函数在模板实例化之上。 。有没有办法为gcc实现这个? 像某些命令行标志或什么?我真的很感激。 谢谢。 解决方案 显然你在这里调用函数的外部版本。 现在定义函数内联。这是一个错误。 编号因为到目前为止函数被声明为extern,这显然是错误的。 否。第二种方法是有效代码。在内联定义后,你调用Function 。 必须在使用之前定义内联函数。围绕这个没有办法 。 我更喜欢在Test体外定义Function。像那样: void函数(); int main(int argc,char * argv []) { 函数(); //这不起作用 } 模板< typename T> 类测试 { public: friend void功能(); }; 模板类测试< int> ;; void函数() {printf(" Function()");的getchar(); } Marcel 所以,你说的是以下不应编译/链接? int foo(); int main() { 返回foo(); } inline int foo(){return 0; } //注意''内联'' V - 请删除大写''A''当通过电子邮件回复 我没有回复最热门的回复,请不要问 现在定义函数内联。这是一个错误。 很抱歉,如果我在模板中定义了 ,那么为什么函数被定义为内联函数,它是如果我在正常的类中定义它不是吗? 嗯,那么这个页面是错误的(或者至少令人困惑): http://www.parashift.com/ c ++ - faq-lit ... html#faq-35.16 它说: "在编译器看到那些神奇的东西之后,它会更好告知 关于朋友的功能。特别是,它会意识到 的朋友行是指自己模板的功能。 这可以消除混乱。 另一种方法是在你声明它是朋友的同时在类中定义友元函数 body。 那么它意味着第一部分中的代码(当函数 函数()在非模板类中定义)应该是错的 ,因此编译器/链接器发出信号,不是吗? > 必须在使用之前定义内联函数。围绕这个没有办法 。 我更喜欢在Test体外定义Function。就像那样: void函数(); int main(int argc,char * argv []) { * * * *功能(); //这不起作用 } 模板< typename T> class Test { public: * * * * friend void Function(); }; 模板类测试< int> ;; void函数() {printf(" Function()" );的getchar(); } Marcel 不幸的是我不能这样做因为这种方法的全部目的 是在Function()函数内部做一些事情。使用 模板,以便在有人使用某个模板 参数实例化Test类时,仅在最终的时定义该函数。 br /> Hi, I have a question about injecting friend functions within templateclasses. My question is specific to gcc (version 3.4.5) used incombination with mingw because this code (or at least code that getsthe same result) works as expected in visualc++. I know that this isprobably not the right behavior for a compiler but it''s the kind ofbehavior I''m searching for so I was hoping there was a way to do thesame thing in gcc.As you know when you define a class (with no template) and within theclass you define a friend function, the function is injected in thescope directly outside of the class itself.--- code ---void Function();int main(int argc, char* argv[]){Function(); // This works}class Test{public:friend void Function() { printf("Function()"); getchar(); }};-- end code --Now I would like to mimic the same behavior with template classes, butthis code doesn''t work:-- code --void Function();int main(int argc, char* argv[]){Function(); // This does not work}template <typename T>class Test{public:friend void Function() { printf("Function()"); getchar(); }};template class Test<int>;-- end code --Specifically I receive an error at link time about Function() notbeing defined, which I guess is due to the compiler not consideringthe definition of Function to be a non-template function and insteadassuming it to be a template function just because it''s defined insideof a template class.Now, the funny thing is that if you move the main() BELOW the explicitinstantiation of the template class Test, this code actually compileand works, that''s why I consider it to be a "bug" becausetheoretically if the definition of Function() is a template function(thus not a normal function) it should never be used for the callinside of the main() function, whether the main() function is belowthe instantiation or not. Am I right?Anyway I''m searching for a way to make the definition of Function()available for the main() function even if the main() function is abovethe template instantiation. Is there a way to accomplish this for gcc?Like some command-line flag or something? I''d really appreciate that.Thanks. 解决方案Obviously you are calling an external version of Function here.Now you define Function inline. this is an error.No. It is because Function is declared as extern so far which isobviously wrong.No. The second approach is valid code. You call Function after it isdefined inline.Inline functions must be defined before their use. There is no wayaround this.I would prefer to define Function outside the body of Test. Like that:void Function();int main(int argc, char* argv[]){Function(); // This does not work}template <typename T>class Test{public:friend void Function();};template class Test<int>;void Function(){ printf("Function()"); getchar(); }MarcelSo, you''re saying that the following should not compile/link?int foo();int main(){return foo();}inline int foo() { return 0; } // note the ''inline''V--Please remove capital ''A''s when replying by e-mailI do not respond to top-posted replies, please don''t askNow you define Function inline. this is an error.Sorry, but why the function is defined as inline if I define it insideof a template and it is not if I define it inside of a normal class?Well, then this page is wrong (or at least confusing): http://www.parashift.com/c++-faq-lit...html#faq-35.16It says:"After the compiler sees that magic stuff, it will be better informedabout the friend functions. In particular, it will realize that thefriend lines are referring to functions that are themselves templates.That eliminates the confusion.Another approach is to define the friend function within the classbody at the same moment you declare it to be a friend."Then it means that the code in the first section (when the functionFunction() is defined within the non-template class) should be wrongtoo, and thus signaled by the compiler/linker, isn''t it?Unluckily I can''t do that because the whole purpose of this approachwas to do something with T inside of the Function() function. Usingtemplates so that the function is defined only at the very finalmoment when someone instantiate the Test class with some templateargument. 这篇关于模板朋友功能注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!