Avoiding duplicate symbols and retaining code in header files

后端 未结 1 1011
甜味超标
甜味超标 2020-12-11 18:22

I have a single global helper function which is used by a bunch of macros in a header file. The intention is to allow the macros to be usable by simply #include

1条回答
  •  情歌与酒
    2020-12-11 18:34

    You are only allowed one function definition in your project unless its marked as inline. You may have as many function declarations as you wish (aka function prototypes).

    Move your function definition to a .cpp file and just leave the declaration in the header file

    void foo(...); // no function body makes this a declaration only
    

    or you could mark it inline:

    inline void foo(...) { /* ... */ } 
    

    inline functions should be small and computationally fast as a general rule.

    0 讨论(0)
提交回复
热议问题