multiple definition error including c++ header file with inline code from multiple sources
<p>I have a c++ header file containing a class.
I want to use this class in several projects, bu I don't want to create a separate library for it, so I'm putting both methods declarations and definitions in the header file:   </p>

<pre><code>// example.h
#ifndef EXAMPLE_H_
#define EXAMPLE_H_
namespace test_ns{

class TestClass{
public:
    void testMethod();
};

void TestClass::testMethod(){
    // some code here...
}

} // end namespace test_ns
#endif
</code></pre>

<p>If inside the same project I include this header from more than one cpp file, I get an error saying "<code>multiple definition of test_ns::TestClass::testMethod()</code>", while if I put the method definition inside the class body this does not happen:</p>

<pre><code>// example.h
#ifndef EXAMPLE_H_
#define EXAMPLE_H_
namespace test_ns{

class TestClass{
public:
    void testMethod(){
        // some code here...
    }
};

} // end namespace test_ns
#endif
</code></pre>

<p>Since the class is defined inside a namespace, shouldn't the two forms be equivalent? Why is the method considered to be defined twice in the first case?</p>
