Let's say I have three files A.cpp, B.cpp, and B.h. A.cpp and B.cpp both include B.h. I defined a free function in B.h (for whatever reasons). Then I get a linker error:
ld: 1 duplicate symbol for architecture x86_64
Which makes sense. The compiler compiles A.cpp and B.cpp separately, and so there are two functions with the same name and parameters.
However, if I declare a class in B.h and define a member-function in this class, it compiles just fine. Why is that? Shouldn't that create the same linker error?