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