是否必须在同一个文件中定义一个C ++内联函数? [英] Must the definition of a C++ inline functions be in the same file?

查看:482
本文介绍了是否必须在同一个文件中定义一个C ++内联函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个名为 ex.h 的头文件中定义了一个 show() ex.cpp 中的函数。我预计这将给我一个错误,因为编译器不知道要替换的位置 show()函数被调用。但是因为我使用的IDE,它工作正常。这是怎么回事?



当我尝试手动编译它时,我给了一个错误, show()

函数的定义(之间的部分)是必须的, {...} )放在头文件中,除非该函数仅在一个 .cpp 文件中使用。

特别是,如果你把内联函数的定义放到一个 .cpp 文件中,并从其他 .cpp 文件,您将从链接器获得未解决的外部错误。



[read more]


I defined a function show() as inlined in a header file called ex.h and the definition of the function inside ex.cpp. I expected that this will give me an error since the compiler will not know what to replace where the show() function is called. But because I'm using an IDE, it worked fine. How could this happen?

And BTW when I tried to compile it manually it gave me an error that the show() is used but not defined.

解决方案

It's imperative that the function's definition (the part between the {...}) be placed in a header file, unless the function is used only in a single .cpp file.
In particular, if you put the inline function's definition into a .cpp file and you call it from some other .cpp file, you'll get an "unresolved external" error from the linker.

[read more]

这篇关于是否必须在同一个文件中定义一个C ++内联函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆