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

后端 未结 5 1463
陌清茗
陌清茗 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:11

    For a global function defined in a header file, declaring it within an un-named namespace should/will also work. According to C++ How to Program by Deitel, in C++ an unnamed namespace is preferable to static.

    So you could do this:

    // \file GlobalFunctions.h
    
    namespace  // an un-named namespace
    {
    
    void GlobalFunctionOne() {...implementation...}
    
    } // end un named namespace
    

提交回复
热议问题