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 link error

Template/class link error

Scheduled Pinned Locked Moved C / C++ / MFC
c++wpfhelpquestion
4 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.
  • E Offline
    E Offline
    EvilSource
    wrote on last edited by
    #1

    First time useing templates and allthough they arnt needed im useing them to get used to templates. Im getting erros with i try to declare my key_table (the class) like key_table< apstring, int> kt; it give the error: error LNK2001: unresolved external symbol "public: __thiscall key_table< class apstring,int>::key_table< class apstring,int>(void)" (??0?$key_table@Vapstring@@H@@QAE@XZ) i dont have anything else in my main except for the statment: key_table< apstring, int> kt; i dont know why it would be complaing about it but if im using the templates wrong please correct me.

    //onetable.h

    #include "assoc.h"

    template< class S, class L> class key_table
    {
    public:
    key_table();
    key_table(const key_table< S,L> &table);

    	int length();
    	bool empty();
    	void store(const S &target);
    	bool get\_string(const S &target);
    	//bool remove(const S &target);
    	key\_table operator =(const key\_table< S,L> &table);
    	
    protected:
    	int table\_length;
    	int current\_string;
    	assoc< S,L> data\[MAX\_TABLE\_SIZE\];
    

    };

    //onetable.cpp

    #include "onetable.h"
    #include template< class S, class L>
    key_table< S,L>::key_table(){
    table_length=0;
    current_string=0;
    }

    template< class S, class L>
    key_table< S,L>::key_table(const key_table< S,L> &table){
    table_length=table.table_length;

    for(int j=0;j< MAX\_TABLE\_SIZE;j++)
    	data\[j\]=table.data\[j\];
    

    }

    template< class S, class L>
    int key_table< S,L>::length(){
    return table_length;
    }

    template< class S, class L>
    bool key_table< S,L>::empty(){
    if(table_length==0)
    return true;
    else
    return false;
    }

    template< class S, class L>
    void key_table< S,L>::store(const S &target){
    int probe = 0;
    bool found = false;

    L item=target.length();
    
    assert(table\_length< MAX\_TABLE\_SIZE);
    assoc a(target, item);
    data\[table\_length+1\]=a;
    ++table\_length;
    

    }

    template< class S, class L>
    bool key_table< S,L>::get_string(const S &item){
    bool success=false;

    if(empty)
    	success=false;
    else if(current\_string=table\_length)
    	success=false;
    else{
    	current\_string++;
    	item=data\[current\_string\].get\_string();
    }
    	
    return success;
    

    }

    D 1 Reply Last reply
    0
    • E EvilSource

      First time useing templates and allthough they arnt needed im useing them to get used to templates. Im getting erros with i try to declare my key_table (the class) like key_table< apstring, int> kt; it give the error: error LNK2001: unresolved external symbol "public: __thiscall key_table< class apstring,int>::key_table< class apstring,int>(void)" (??0?$key_table@Vapstring@@H@@QAE@XZ) i dont have anything else in my main except for the statment: key_table< apstring, int> kt; i dont know why it would be complaing about it but if im using the templates wrong please correct me.

      //onetable.h

      #include "assoc.h"

      template< class S, class L> class key_table
      {
      public:
      key_table();
      key_table(const key_table< S,L> &table);

      	int length();
      	bool empty();
      	void store(const S &target);
      	bool get\_string(const S &target);
      	//bool remove(const S &target);
      	key\_table operator =(const key\_table< S,L> &table);
      	
      protected:
      	int table\_length;
      	int current\_string;
      	assoc< S,L> data\[MAX\_TABLE\_SIZE\];
      

      };

      //onetable.cpp

      #include "onetable.h"
      #include template< class S, class L>
      key_table< S,L>::key_table(){
      table_length=0;
      current_string=0;
      }

      template< class S, class L>
      key_table< S,L>::key_table(const key_table< S,L> &table){
      table_length=table.table_length;

      for(int j=0;j< MAX\_TABLE\_SIZE;j++)
      	data\[j\]=table.data\[j\];
      

      }

      template< class S, class L>
      int key_table< S,L>::length(){
      return table_length;
      }

      template< class S, class L>
      bool key_table< S,L>::empty(){
      if(table_length==0)
      return true;
      else
      return false;
      }

      template< class S, class L>
      void key_table< S,L>::store(const S &target){
      int probe = 0;
      bool found = false;

      L item=target.length();
      
      assert(table\_length< MAX\_TABLE\_SIZE);
      assoc a(target, item);
      data\[table\_length+1\]=a;
      ++table\_length;
      

      }

      template< class S, class L>
      bool key_table< S,L>::get_string(const S &item){
      bool success=false;

      if(empty)
      	success=false;
      else if(current\_string=table\_length)
      	success=false;
      else{
      	current\_string++;
      	item=data\[current\_string\].get\_string();
      }
      	
      return success;
      

      }

      D Offline
      D Offline
      Derek Waters
      wrote on last edited by
      #2

      You can't declare the implementation of template classes in your .cpp files. Try moving all the functions inline into your template classes .h file and see how you go. Hope this helps. ------------------------ Derek Waters derek@lj-oz.com

      E 1 Reply Last reply
      0
      • D Derek Waters

        You can't declare the implementation of template classes in your .cpp files. Try moving all the functions inline into your template classes .h file and see how you go. Hope this helps. ------------------------ Derek Waters derek@lj-oz.com

        E Offline
        E Offline
        EvilSource
        wrote on last edited by
        #3

        but how do i keep from having one big .h file, i do not wish to declare all my classes in the .h file, thats what the .cpp is for? is it proper/normal programing to defile all the functions in the .h?

        M 1 Reply Last reply
        0
        • E EvilSource

          but how do i keep from having one big .h file, i do not wish to declare all my classes in the .h file, thats what the .cpp is for? is it proper/normal programing to defile all the functions in the .h?

          M Offline
          M Offline
          Michael Dunn
          wrote on last edited by
          #4

          You've hit a limitation of VC and templates, the template code has to be visible in every CPP file, so it has to be in header files. --Mike-- "Jobs that don't allow you to visit the Lounge 25 times a day at the minimum are not worth having anyway."   -- Nish, 3/28/2002 My really out-of-date homepage Sonork - 100.10414 AcidHelm Big fan of Alyson Hannigan and Jamie Salé.

          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