devxlogo

Avoid Link Errors in VC++ While Using Templates

Avoid Link Errors in VC++ While Using Templates

If you have coded a template class and use it from VC++, you will get a LNK2001 error message on template member functions. This is because Microsoft deviates a bit from C++ specifications. To reproduce the problem, do the following:

1. Declare a template class.

 //HastTable.htemplate l;class HastTable{public:   static float bar;   void foo();}//HastTable.cppint HashTable::bar;  //allocate the static variable in templateclass.template HastTable::foo(){//do something}
2. Try to use HashTable from your main program.
#include

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist