Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. templates with a nice unresolved external

templates with a nice unresolved external

Scheduled Pinned Locked Moved C / C++ / MFC
csharpc++helpvisual-studiowpf
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    Ylis
    wrote on last edited by
    #1

    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! :)

    T B N 3 Replies Last reply
    0
    • Y Ylis

      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! :)

      T Offline
      T Offline
      ThatsAlok
      wrote on last edited by
      #2

      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
      1 Reply Last reply
      0
      • Y Ylis

        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! :)

        B Offline
        B Offline
        Bob Ciora
        wrote on last edited by
        #3

        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 like inlines, 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

        Y 1 Reply Last reply
        0
        • Y Ylis

          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! :)

          N Offline
          N Offline
          Nemanja Trifunovic
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • B Bob Ciora

            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 like inlines, 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

            Y Offline
            Y Offline
            Ylis
            wrote on last edited by
            #5

            Thanks! :)

            B 1 Reply Last reply
            0
            • Y Ylis

              Thanks! :)

              B Offline
              B Offline
              Bob Ciora
              wrote on last edited by
              #6

              Soitanly! Glad to help :) Bob Ciora

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups