0

"file.h"

template <class ClassType>
class NewClass
    {
    protected:

    public:
        NewClass() {}
        ~NewClass() {}

        static long INITIAL_SIZE;
    };

"file.cpp"

template<class ClassType>
long NewClass<ClassType>::INITIAL_SIZE{ 50 };

"usage file"

in the main function

std::cout << NewClass<char>::INITIAL_SIZE << std::endl;

Can someone please tell me why this gives me a "lnk2001 unresolved external symbol" error?

So whenever I moved:

template<class ClassType>
long NewClass<ClassType>::INITIAL_SIZE{ 50 };

to the header file it would throw different error... but once I changed {50} to = 50:

template<class ClassType>
long NewClass<ClassType>::INITIAL_SIZE = 50;

it worked. I don't understand why, but the problem was the way I was setting the variable.

Michael
  • 73
  • 3
  • If the template class is located in lib (or dll), it should be used also in the dll with the `char`, otherwise the template class won't be compiled with the `char` argument. – SHR Sep 05 '15 at 19:42
  • And for the duplicate issue, you can't put the static declaration in the header file, so I can't see how it suppose to answer you're question. – SHR Sep 05 '15 at 19:45
  • @SHR _"you can't put the static declaration in the header file,"_ Wrong. You well can do so for templates. Also DLL is out of question, where did you get that from? – πάντα ῥεῖ Sep 05 '15 at 19:47
  • he got it from the fact that when I do declare it in the header file it throws an error saying static none const variables cant be initiated in header files. – Michael Sep 06 '15 at 11:15

0 Answers0