C++: Multiple definition error for global functions in the header file

后端 未结 5 1444
陌清茗
陌清茗 2021-02-12 17:52

This function is global and is defined in the header file (temporarily I want to keep it there).

The header file also constitutes a particular class which has i

5条回答
  •  甜味超标
    2021-02-12 18:24

    You have 2 options:

    Mark it as inline, as explained by nbt, or as static.

    inline will take the implementation of the global function from the source and copy it into wherever the function is called.

    inline void global_func ()
    {
    ...
    }
    

    static will tell the linker to not copy the code into the new object file but rather only reference it in the original.

    static void global_func ()
    {
    ...
    }
    

提交回复
热议问题