c++ - C++ 内联函数的定义必须在同一个文件中吗?

标签 c++ function compiler-construction inline

我将函数 show() 定义为内联在名为 ex.h 的头文件中,并在 ex.cpp 中定义函数>。我预计这会给我一个错误,因为编译器不知道在调用 show() 函数的位置替换什么。但是因为我使用的是 IDE,所以效果很好。怎么会这样?

顺便说一句,当我尝试手动编译它时,它给了我一个错误,即 show() 已使用但未定义。

最佳答案

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++ - C++ 内联函数的定义必须在同一个文件中吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9338152/

相关文章:

c++ - 为什么enable_shared_from_this嵌入一个弱指针而不是直接嵌入引用计数器呢?

c++ - 在开始时使用所有可用数据构建大型(ish)无序集

c - 程序中的杂散字符 ("error: stray ’\223’")

c++ - 静态编译器和运行时编译器等术语的实际含义是什么?

c++ - Qt自定义QPushButton不在布局上显示

c++ - 使用 Rabin Karp 进行模式搜索

c - 我的语法中的函数定义有问题

c - 在 main 以外的函数中定义的变量的生命周期

c - 当我在 Quicksort5 函数中激活该行时,它的排序效果不佳。但为什么?

java - 测试编译器