templates with a nice unresolved external
-
I'm trying to understand templates and has therefor tried to make a linked list. The code is probably full of more errors but the one I can't seem to find now relates to an unresolved external. I don't have much experience with C++ in general so it could be something really basic. I use Visual Studio 2003 .NET and this is the code: LList.h
#ifndef LIST_H_ #define LIST_H_ template< class T > class Element { public: Element *next; Element *previous; T data; void Remove(); }; template< class T > class LList { public: LList(); T& operator []( int p_elem ); void Add( T p_data ); protected: int m_noElems; Element< T > m_elements; }; #endif
LList.cpp#include "list.h" template< class T > void Element< T >::Remove() { if( this->previous != NULL ) this->previous->next = this->next; if( this->next != NULL ) this->next->previous = this->previous; delete this; } template< class T > LList< T >::List() { m_noElems = 0; } template< class T > T& LList< T >::operator []( int p_elem ) { if( p_elem > m_noElems ) return NULL; Element< T > *curEle; for( int i = 0; i < p_elem; i++ ) curEle = m_elements.next; return curEle->data; } template< class T > void LList< T >::Add( T p_data ) { Element< T > *curEle; for( int i = 0: i < m_noElems; i++ ) curEle = m_elements.next; curEle->next = new Element; curEle->next->next = NULL; curEle->next->previous = curEle; curEle->next->data = p_data; m_noElems++; }
main.cpp#include "list.h" int main() { LList< int > test; }
error: list error LNK2019: unresolved external symbol "public: __thiscall LList::LList(void)" (??0?$LList@H@@QAE@XZ) referenced in function _main Any help appriciated! :) -
I'm trying to understand templates and has therefor tried to make a linked list. The code is probably full of more errors but the one I can't seem to find now relates to an unresolved external. I don't have much experience with C++ in general so it could be something really basic. I use Visual Studio 2003 .NET and this is the code: LList.h
#ifndef LIST_H_ #define LIST_H_ template< class T > class Element { public: Element *next; Element *previous; T data; void Remove(); }; template< class T > class LList { public: LList(); T& operator []( int p_elem ); void Add( T p_data ); protected: int m_noElems; Element< T > m_elements; }; #endif
LList.cpp#include "list.h" template< class T > void Element< T >::Remove() { if( this->previous != NULL ) this->previous->next = this->next; if( this->next != NULL ) this->next->previous = this->previous; delete this; } template< class T > LList< T >::List() { m_noElems = 0; } template< class T > T& LList< T >::operator []( int p_elem ) { if( p_elem > m_noElems ) return NULL; Element< T > *curEle; for( int i = 0; i < p_elem; i++ ) curEle = m_elements.next; return curEle->data; } template< class T > void LList< T >::Add( T p_data ) { Element< T > *curEle; for( int i = 0: i < m_noElems; i++ ) curEle = m_elements.next; curEle->next = new Element; curEle->next->next = NULL; curEle->next->previous = curEle; curEle->next->data = p_data; m_noElems++; }
main.cpp#include "list.h" int main() { LList< int > test; }
error: list error LNK2019: unresolved external symbol "public: __thiscall LList::LList(void)" (??0?$LList@H@@QAE@XZ) referenced in function _main Any help appriciated! :)have you added
List.cpp
in your project i.e. In FileView| right Click| add Item|add existing item | add list.h and list.cpp in yoour console project.
"I Think this Will Help"
visit me at http://www.thisisalok.tk
-
I'm trying to understand templates and has therefor tried to make a linked list. The code is probably full of more errors but the one I can't seem to find now relates to an unresolved external. I don't have much experience with C++ in general so it could be something really basic. I use Visual Studio 2003 .NET and this is the code: LList.h
#ifndef LIST_H_ #define LIST_H_ template< class T > class Element { public: Element *next; Element *previous; T data; void Remove(); }; template< class T > class LList { public: LList(); T& operator []( int p_elem ); void Add( T p_data ); protected: int m_noElems; Element< T > m_elements; }; #endif
LList.cpp#include "list.h" template< class T > void Element< T >::Remove() { if( this->previous != NULL ) this->previous->next = this->next; if( this->next != NULL ) this->next->previous = this->previous; delete this; } template< class T > LList< T >::List() { m_noElems = 0; } template< class T > T& LList< T >::operator []( int p_elem ) { if( p_elem > m_noElems ) return NULL; Element< T > *curEle; for( int i = 0; i < p_elem; i++ ) curEle = m_elements.next; return curEle->data; } template< class T > void LList< T >::Add( T p_data ) { Element< T > *curEle; for( int i = 0: i < m_noElems; i++ ) curEle = m_elements.next; curEle->next = new Element; curEle->next->next = NULL; curEle->next->previous = curEle; curEle->next->data = p_data; m_noElems++; }
main.cpp#include "list.h" int main() { LList< int > test; }
error: list error LNK2019: unresolved external symbol "public: __thiscall LList::LList(void)" (??0?$LList@H@@QAE@XZ) referenced in function _main Any help appriciated! :)With templates, all of the template methods have to be defined in the template's Header file. Alternately, if you want to put them into a different file, then you'll have to
#include
that file in your template's .h file. The compiler treats templates likeinlines
, so it needs to understand how to expand your instantiations of the template. By putting the template's methods in another file that's not in the "include path" of your source, it doesn't know how to expand LList<T> for any particular <T>. Try adding#include "LList.cpp"
at the end of your header file (just before the#endif
that closes out your#ifndef LIST_H__
). *EDIT* If you#include "LList.cpp"
, do *not* add LList.cpp to your project.*EDIT* Bob Ciora -
I'm trying to understand templates and has therefor tried to make a linked list. The code is probably full of more errors but the one I can't seem to find now relates to an unresolved external. I don't have much experience with C++ in general so it could be something really basic. I use Visual Studio 2003 .NET and this is the code: LList.h
#ifndef LIST_H_ #define LIST_H_ template< class T > class Element { public: Element *next; Element *previous; T data; void Remove(); }; template< class T > class LList { public: LList(); T& operator []( int p_elem ); void Add( T p_data ); protected: int m_noElems; Element< T > m_elements; }; #endif
LList.cpp#include "list.h" template< class T > void Element< T >::Remove() { if( this->previous != NULL ) this->previous->next = this->next; if( this->next != NULL ) this->next->previous = this->previous; delete this; } template< class T > LList< T >::List() { m_noElems = 0; } template< class T > T& LList< T >::operator []( int p_elem ) { if( p_elem > m_noElems ) return NULL; Element< T > *curEle; for( int i = 0; i < p_elem; i++ ) curEle = m_elements.next; return curEle->data; } template< class T > void LList< T >::Add( T p_data ) { Element< T > *curEle; for( int i = 0: i < m_noElems; i++ ) curEle = m_elements.next; curEle->next = new Element; curEle->next->next = NULL; curEle->next->previous = curEle; curEle->next->data = p_data; m_noElems++; }
main.cpp#include "list.h" int main() { LList< int > test; }
error: list error LNK2019: unresolved external symbol "public: __thiscall LList::LList(void)" (??0?$LList@H@@QAE@XZ) referenced in function _main Any help appriciated! :)To understand what is giong on, read this article[^]. Ylis wrote: I don't have much experience with C++ in general so it could be something really basic. I hope you don't mind a piece of advice :) Refrain from trying to learn how to make template libraries, at least at this stage of learning C++. This is the most complex area of a very complex programming language, and chances are you are never going to do it in practice anyway. Just learn how to use template libraries - start with STL and Boost; it is not hard, and it will improve your productivity significantly.
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
-
With templates, all of the template methods have to be defined in the template's Header file. Alternately, if you want to put them into a different file, then you'll have to
#include
that file in your template's .h file. The compiler treats templates likeinlines
, so it needs to understand how to expand your instantiations of the template. By putting the template's methods in another file that's not in the "include path" of your source, it doesn't know how to expand LList<T> for any particular <T>. Try adding#include "LList.cpp"
at the end of your header file (just before the#endif
that closes out your#ifndef LIST_H__
). *EDIT* If you#include "LList.cpp"
, do *not* add LList.cpp to your project.*EDIT* Bob Ciora