function … has already a body & function template has already been defined

前端 未结 3 1674
迷失自我
迷失自我 2020-12-22 13:35

I have this header file:

Utility.h:

#pragma once

#include 
#include 
#include 
#includ         


        
3条回答
  •  滥情空心
    2020-12-22 14:31

    There are main two problems:

    • You have placed intended header code in a ".cpp" file, which your IDE by default will treat as a main source code file (translation unit).

    • You are including non-inline function definitions (the class member functions) in the global namespace in a header. When that header is included in two or more translation units you will get a One Definition Rule (ODR) violation. In practice the linker will complain.

    To fix this:

    • Change the filename extension from ".cpp" to e.g. ".hpp" or just plain ".h". It is code intended for header file use. The filename extension should reflect that, instead of misleading.

    • Declare the functions inline, or place definitions within the class definition.


    In other news, possibly one of the clocks, from the standard library, will serve your purpose so that you don't have to include in a header. It does drag in a zillion unreasonable macros. Including, by default, lowercase min and max macros, so this code is very ungood as given.

提交回复
热议问题