Pergunta

I have a perfectly working code:

template <typename ...Ts>
class ThreadImplementation {
    ...
    void launch(){...}
    ~ThreadImplementation(){...}
};
...
ThreadImplementation<Ts...> *t = new ThreadImplementation<Ts...>(debugName,func,args...);
...
// std::vector< std::unique_ptr< ThreadImplementation<> > >  thread_107_allThreads;
thread_107_allThreads.emplace_back(t);
...

where thread_107_allThreads as of now stores only the ones with no template parameters, hence ThreadImplementation<> in the declaration

now if I add above all

class AnyThreadImplementation {
    public:
    virtual void launch() = 0;
    virtual ~AnyThreadImplementation() = 0;
};

and make ONLY the following 3 changes (no casting, no change in use, nor in test):

class ThreadImplementation : public AnyThreadImplementation {

    virtual void launch(){...}

    virtual ~ThreadImplementation(){...}

(although the last 2 changes does not affect the result ie: I get the same error if I miss the virtual's in front of the actual implementations in the subclass)

and I get:

/tmp/test-fuPonc.o: In function `_ZN20ThreadImplementationIJEEC2EPKcPFvvE':
test.cpp:(.text._ZN20ThreadImplementationIJEEC2EPKcPFvvE[_ZN20ThreadImplementationIJEEC2EPKcPFvvE]+0xbb): undefined reference to `AnyThreadImplementation::~AnyThreadImplementation()'
/tmp/test-fuPonc.o: In function `_ZN20ThreadImplementationIJEED2Ev':
test.cpp:(.text._ZN20ThreadImplementationIJEED2Ev[_ZN20ThreadImplementationIJEED2Ev]+0x233): undefined reference to `AnyThreadImplementation::~AnyThreadImplementation()'
test.cpp:(.text._ZN20ThreadImplementationIJEED2Ev[_ZN20ThreadImplementationIJEED2Ev]+0x267): undefined reference to `AnyThreadImplementation::~AnyThreadImplementation()'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Why ?

Foi útil?

Solução

Pure virtual destructor should be defined too.

Relevant quote from N3376 12.4/9

A destructor can be declared virtual or pure virtual; if any objects of that class or any derived class are created in the program, the destructor shall be defined.

class AnyThreadImplementation {
    public:
    virtual void launch() = 0;
    virtual ~AnyThreadImplementation() = 0;
};

AnyThreadImplementation::~AnyThreadImplementation() {}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top