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. Template Class [modified]

Template Class [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
visual-studiowpfcomhelp
7 Posts 3 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.
  • M Offline
    M Offline
    Mikey_H
    wrote on last edited by
    #1

    I am using templates for a class for the first time and I cannot figure out what I am doing wrong. Any help would be greatly appreciated. This code will generate LNK2019 Unresolved External

    // Test AssocArray
    AssocArray<Vector3> myArray;

    Class Definition

    template <class TValue>
    class AssocArray
    {
    public:
    AssocArray();
    AssocArray(int size, int resizeBy);

    int	GetCount()	{ return m\_nItems; }
    
    void	SetNewSize(int size, bool copy = true);
    void	IncreaseSize(int size, bool copy = true);
    
    TValue	operator \[\](int i);
    TValue	operator \[\](string s);
    

    private:
    int m_nItems;
    int m_nMaxItems;
    int m_nResizeBy;

    KeyValuePair<string, TValue>\*	m\_array;
    

    };

    Class Constructor

    template <class TValue>
    AssocArray<TValue>::AssocArray()
    {
    m_nItems = 0;
    m_nMaxItems = 2;
    m_nResizeBy = 2;
    m_array = new KeyValuePair[2];
    }

    modified on Sunday, May 3, 2009 5:28 AM

    N 1 Reply Last reply
    0
    • M Mikey_H

      I am using templates for a class for the first time and I cannot figure out what I am doing wrong. Any help would be greatly appreciated. This code will generate LNK2019 Unresolved External

      // Test AssocArray
      AssocArray<Vector3> myArray;

      Class Definition

      template <class TValue>
      class AssocArray
      {
      public:
      AssocArray();
      AssocArray(int size, int resizeBy);

      int	GetCount()	{ return m\_nItems; }
      
      void	SetNewSize(int size, bool copy = true);
      void	IncreaseSize(int size, bool copy = true);
      
      TValue	operator \[\](int i);
      TValue	operator \[\](string s);
      

      private:
      int m_nItems;
      int m_nMaxItems;
      int m_nResizeBy;

      KeyValuePair<string, TValue>\*	m\_array;
      

      };

      Class Constructor

      template <class TValue>
      AssocArray<TValue>::AssocArray()
      {
      m_nItems = 0;
      m_nMaxItems = 2;
      m_nResizeBy = 2;
      m_array = new KeyValuePair[2];
      }

      modified on Sunday, May 3, 2009 5:28 AM

      N Offline
      N Offline
      NeoAks007
      wrote on last edited by
      #2

      What's the exact error that is displayed on Output window???

      M 1 Reply Last reply
      0
      • N NeoAks007

        What's the exact error that is displayed on Output window???

        M Offline
        M Offline
        Mikey_H
        wrote on last edited by
        #3

        Error 1 error LNK2019: unresolved external symbol "public: __thiscall AssocArray<class Vector3>::AssocArray<class Vector3>(void)" (??0?$AssocArray@VVector3@@@@QAE@XZ) referenced in function "public: void __thiscall EngineRoot::InitializeLater(void)" (?InitializeLater@EngineRoot@@QAEXXZ) EngineRoot.obj Error 2 fatal error LNK1120: 1 unresolved externals

        modified on Sunday, May 3, 2009 9:39 PM

        J 1 Reply Last reply
        0
        • M Mikey_H

          Error 1 error LNK2019: unresolved external symbol "public: __thiscall AssocArray<class Vector3>::AssocArray<class Vector3>(void)" (??0?$AssocArray@VVector3@@@@QAE@XZ) referenced in function "public: void __thiscall EngineRoot::InitializeLater(void)" (?InitializeLater@EngineRoot@@QAEXXZ) EngineRoot.obj Error 2 fatal error LNK1120: 1 unresolved externals

          modified on Sunday, May 3, 2009 9:39 PM

          J Offline
          J Offline
          Joe Woodbury
          wrote on last edited by
          #4

          Is the constructor in the same file as the class definition? (Hint: it isn't.) (If you're wondering why it doesn't give you a compilation error, it's because the compiler doesn't know if you have a class template specialization defined somewhere.)

          Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

          M 1 Reply Last reply
          0
          • J Joe Woodbury

            Is the constructor in the same file as the class definition? (Hint: it isn't.) (If you're wondering why it doesn't give you a compilation error, it's because the compiler doesn't know if you have a class template specialization defined somewhere.)

            Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

            M Offline
            M Offline
            Mikey_H
            wrote on last edited by
            #5

            Constructor and all member functions are in a separate file to the definition. There is no template specialization in the code.

            J 1 Reply Last reply
            0
            • M Mikey_H

              Constructor and all member functions are in a separate file to the definition. There is no template specialization in the code.

              J Offline
              J Offline
              Joe Woodbury
              wrote on last edited by
              #6

              That's my point; put everything into one file. By their nature, template classes are nothing but definition.

              Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

              M 1 Reply Last reply
              0
              • J Joe Woodbury

                That's my point; put everything into one file. By their nature, template classes are nothing but definition.

                Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

                M Offline
                M Offline
                Mikey_H
                wrote on last edited by
                #7

                Thank you, that has solved the problem

                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