c++ undefined reference to vtable exception : <p>I've searched many answers but none of them can solve my problem, I'm new to c++, this issue is quite wired to me. Below is a simplified extraction of my code.</p>

<p>TestHeader.h:</p>

<pre><code>#ifndef NAMESPACE_TESTHEADER_H_
#define NAMESPACE_TESTHEADER__H_

namespace Namespace {

class TestHeader {

    public:

    TestHeader(const std::string&amp; str) : anyString_(str) { }

    virtual std::string methodOne(const std::string&amp; param) const;

    virtual ~TestHeader() { anyString_.clear(); }

    protected:
        std::string anyString_;
};
}

#endif //NAMESPACE_TESTHEADER__H_
</code></pre>

<p>TestHeader.cpp:</p>

<pre><code>#include "TestHeader.h"

using namespace std;

namespace Namespace {

TestHeader::TestHeader(const std::string&amp; str):anyString_(str) { &lt;do something&gt;; }

std::string TestHeader::methodOne(const std::string&amp; param) const
{
    return &lt;A string&gt;;
}

TestHeader::~TestHeader() {
   anyString_.clear();
}
}
</code></pre>

<p>What I did was simply call this line in any other .cpp in my package:</p>

<pre><code>#include "TestHeader.h"
TestHeader testHeader("whatever");
</code></pre>

<p>The build failed by throwing </p>

<pre><code>error: undefined reference to 'vtable for Namespace::TestHeader'
the vtable symbol may be undefined because the class is missing its key function
</code></pre>

<p><strong>The most weird thing is: if I comment out <code>virtual std::string methodOne(const std::string&amp; str) const;</code> in header and its implementation in .cpp, OR, if I comment out <code>: anyString_(str)</code> after constructor and <code>anyString_.clear();</code> in destructor together in header only, the build will succeed.</strong></p>
