What is the reasoning for the multiple definitions error?
Because you are defining the functions in the header file and when you include the header in translation units a copy of the function is created in each translation unit, thus leading to multiple definitions and violation of one definition rule.
What is the solution?
You can define the functions separately but in a cpp file. You declare functions in header file and define them in source cpp file.
Why first example works?
The only standard compliant way to bypass one definition rule is to use inline
functions. When you define the functions inside the class body, they are implicitly inline
and the program can successfully bypass one definition rule and the multiple definition linking error.