inline函数必须定义在头文件吗

本文通过一个具体的示例,介绍了C++中inline函数的使用误区及其导致的链接错误。详细解释了为什么将inline函数定义放在源文件而不是头文件会引发未解析外部符号的问题,并给出了解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前不久在写代码的时候遇到一个link错误,代码的原型如下所示,基本就是定义了一个基类和派生类,在派生类的一个成员函数中用到了基类定义的一个内联函数。

  1. // base.h  
  2. class Base  
  3. {  
  4. protected:  
  5.    void fun();  
  6. };  
  7.   
  8. // base.cpp  
  9. #include “base.h”  
  10. inline void Base::fun()  
  11. {}  
  12.   
  13. // derived.h  
  14. #include “base.h”  
  15. class Derived: public Base  
  16. {  
  17. public:  
  18.    void g();  
  19. };  
  20.   
  21. // derived.cpp  
  22. void Derived::g()  
  23. {  
  24.    fun(); //VC2008: error LNK2019: unresolved external symbol  
  25. }  
  1. // base.h  
  2. class Base  
  3. {  
  4. protected:  
  5.    void fun();  
  6. };  
  7.   
  8. // base.cpp  
  9. #include “base.h”  
  10. inline void Base::fun()  
  11. {}  
  12.   
  13. // derived.h  
  14. #include “base.h”  
  15. class Derived: public Base  
  16. {  
  17. public:  
  18.    void g();  
  19. };  
  20.   
  21. // derived.cpp  
  22. void Derived::g()  
  23. {  
  24.    fun(); //VC2008: error LNK2019: unresolved external symbol  
  25. }  
// base.h
class Base
{
protected:
   void fun();
};

// base.cpp




#include "base.h" inline void Base::fun() {} // derived.h #include "base.h" class Derived: public Base { public:    void g(); }; // derived.cpp void Derived::g() {    fun(); //VC2008: error LNK2019: unresolved external symbol }

写这个内联函数的时候也没细想,结果违反了inline函数的要求。所谓内联函数,就是编译器将函数定义({…}之间的内容)在函数调用处展开,藉此来免去函数调用的开销。如果这个函数定义在头文件中,所有include该头文件的编译单元都可以正确找到函数定义。然而,如果内联函数fun()定义在某个编译单元A中,那么其他编译单元中调用fun()的地方将无法解析该符号,因为在编译单元A生成目标文件A.obj后,内联函数fun()已经被替换掉,A.obj中不再有fun这个符号,链接器自然无法解析。

所以,如果一个inline函数会在多个源文件中被用到,那么必须把它定义在头文件中。在C++中,这意味着如果inline函数具有public或者protected访问属性,你就应该这么做。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值