Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Lineker has template class problem (LNK2001)

Status
Not open for further replies.

IonelBurtan

Programmer
May 25, 2001
601
RO
I haven't used template classes for quite a while and now, when I am tring to use it I get a linker problem.

I made a template class


template <class T> class CSearchAlgorithms
{
public:
CSearchAlgorithms(T* pArray, int nCount, T nLookedFor);
virtual ~CSearchAlgorithms();
void LinearSearch();
void BinarySearch();

private:
T* m_pArray;
int m_nCount;
T m_nLookedFor;
};

and place it in SearchAlgorithms.h
the body of the template class is placed in SearchAlgorithms.cpp with funtion declaration like this
like this

template <class T>
void CSearchAlgorithms<T>::LinearSearch()
{
...
}


Then I try to use the template class from the main program. I include SearchAlgorithms.h, and I instantiate with

...
CSearchAlgorithms<int> sa(nArray, nArraySize, nLookedFor);
sa.LinearSearch();
sa.BinarySearch();
...

All compiles well but the linker gives me 4 errors (i have 4 methods in the template class) like this:

SearchProj.obj : error LNK2001: unresolved external symbol &quot;public: virtual __thiscall CSearchAlgorithms<int>::~CSearchAlgorithms<int>(void)&quot; (??1?$CSearchAlgorithms@H@@UAE@XZ)
SearchProj.obj : error LNK2001: unresolved external symbol &quot;public: void __thiscall CSearchAlgorithms<int>::BinarySearch(void)&quot; (?BinarySearch@?$CSearchAlgorithms@H@@QAEXXZ)
SearchProj.obj : error LNK2001: unresolved external symbol &quot;public: void __thiscall CSearchAlgorithms<int>::LinearSearch(void)&quot; (?LinearSearch@?$CSearchAlgorithms@H@@QAEXXZ)
SearchProj.obj : error LNK2001: unresolved external symbol &quot;public: __thiscall CSearchAlgorithms<int>::CSearchAlgorithms<int>(int *,int,int)&quot; (??0?$CSearchAlgorithms@H@@QAE@PAHHH@Z)
Debug/SearchProj.exe : fatal error LNK1120: 4 unresolved externals

I am sure I am missig something but i do not remember what
Tnx,

s-)

Blessed is he who in the name of justice and goodwill, sheperds the weak through the valley of darkness...
 
Common problems:

1) A project that uses function inlining yet defines the functions in a .cpp file rather than in the header file can cause LNK2001.

2) Attempting to reference functions or data that don't have external linkage can cause LNK2001. In C++, inline functions and const data have internal linkage unless explicitly specified as extern.

3) A missing function body or variable can cause LNK2001.

4) Calling a function with parameter types that do not match those in the function declaration can cause LNK2001. Name decoration incorporates the parameters of a function into the final decorated function name.

5) When using C++, including a function prototype in a class definition and failing to include the implementation of the function for that class can cause LNK2001.

All 5 above are taken from MSDN. I've had this problem too, but solved it by implementing the destructor (that's it, i forgot :-D )
 
Tnx pal, but I solved it. It is a non-compliance of the Visual C++ 6 with the C++ standard for templates.

In MSDN they describe it in the article
PRB: LNK2001 on Template Member Functions.

Both of the work-arounds proposed work, but i chose the first, that is to put both the declaration and implementation in the same file and name it .h or no extension

Tnx,


s-)

Blessed is he who in the name of justice and goodwill, sheperds the weak through the valley of darkness...
 
I had this same problem. I tried putting all the inplementation of the class in the header file. It worked partly. But MFC needs that MESSAGE_MAP declaration be in the cpp file and so the compiler is now complaining. Has anyone found a workaround for this?

Thanks a lot
Mike
 
If you want to put the implementation in the .cpp file and you know what the template parameters will be, put the following at the top of your .cpp file:

#pragma warning(disable: 4660)

template class YourTemplate<double>;
template class YourTemplate<complex<double> >;

etc

JonnoA
 
All that you are talking about is clear, but I have more interesting case.

I am trying compile my c++ application under .NET. And get a lot of errors like following:

McAttClip.obj : error LNK2001: unresolved external symbol &quot;__declspec(dllimport) public: __thiscall std::basic_string<unsigned short,struct std::char_traits<unsigned short>,class std::allocator<unsigned short> >::~basic_string<unsigned short,struct std::char_traits<unsigned short>,class std::allocator<unsigned short> >(void)&quot; (__imp_??1?$basic_string@GU?$char_traits@G@std@@V?$allocator@G@2@@std@@QAE@XZ)

I wonder that it wants?
basic_string???!!!
I use wstring and have no problem until this moment.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top