0

I'm facing LNK2019 error: unresolved external symbol on some_container methods (including constructor and destructor) while trying to build my project.

This comes from my template class:

some_container.hpp

#include <unordered_set>
namespace name_space {

    template<typename T> class some_container {
        private: static std::unordered_set<T> some_set;
        public:
            some_container();
            ~some_container();
            static const bool insert(T item);
    }
}

some_container.cpp

#include "some_container.hpp"

using namespace name_space;

template<typename T>
some_container<T>::some_container() {
}

template<typename T>
some_container<T>::~some_container() {
}

template<typename T>
const bool some_container<T>::insert(T item) {

     auto pair = some_container::some_set.insert(T); //Something wrong here?
     return pair.second;
}

When some class inherits, this is how it's done:

class MyClass : private some_container<std::vector<int>*> {

    //...
}

EDIT:

I tried moving all the implementation to my .hpp file, outside class and within the namespace. But now I'm getting another error:

LNK2001: unresolved external symbol on some_set (static data member inside some_container) class.

EDIT 2: Above issue has been fixed by changing it to "inline static". I'm not sure if it's the best approach though.

Victor Vector
  • 31
  • 1
  • 4
  • 3
    Does this answer your question? [Why can templates only be implemented in the header file?](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – dewaffled Oct 18 '21 at 16:52
  • Thanks for your reply. I did try moving my implementation to header file, but there's just one error - LNK2001: unresolved external symbol "private static class.." which points to some_set in some_container class. – Victor Vector Oct 18 '21 at 17:38
  • EDIT: Above issue has been fixed by changing it to "inline static". Not sure if it's the best approach. – Victor Vector Oct 18 '21 at 18:45

0 Answers0