why c++ templated class linking error happened?

Viewed 19

I have problem with linking templated class. How to correct do that? I have next classes:

A.h

class A
{
    int _num;
public:
    A(int num);
    virtual ~A();

    int getNum();
};

a.cpp:

#include <iostream>
#include "a.h"

A::A(int num)
    : _num(num)
{
    std::cout << "A" << std::endl;
}

A::~A()
{}

int A::getNum()
{
    return _num;
}

b.h:

#include "a.h"
#include <memory>

template<class T>
class B : public A
{
    std::unique_ptr<T> _data;
public:
    B();
    ~B();

};

b.cpp:

#include "b.h"
#include <iostream>

template <class T>
B<T>::B()
    : A(2)
    , _data(std::make_unique<T>())
{
    std::cout << "B" << std::endl;
}

template <class T>
B<T>::~B()
{}

c.h:

#include "b.h"

struct SomeData
{
    int d1;
    int d2;
};

class C : public B<SomeData>
{
public:
    C();
    ~C();
    bool run();
};

c.cpp:

#include <iostream>
#include "c.h"

C::C()
    :B<SomeData>()
{}

C::~C()
{}

bool C::run()
{
    return true;
}

main.cpp:

#include <iostream>
#include "c.h"

int main()
{
    std::cout << "Hello World!\n";
    C myTest;
}

When compiling it is next errors:

1>c.obj : error LNK2019: unresolved external symbol "public: __thiscall B::B(void)" (??0?$B@USomeData@@@@QAE@XZ) referenced in function "public: __thiscall C::C(void)" (??0C@@QAE@XZ) 1>c.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall B::~B(void)" (??1?$B@USomeData@@@@UAE@XZ) referenced in function "public: virtual __thiscall C::~C(void)" (??1C@@UAE@XZ) 1>C:\Users\user\Projects\NewAggregatorProject\TestMe\Debug\TestMe.exe : fatal error LNK1120: 2 unresolved externals

How to make it correct?

0 Answers
Related