1
#ifndef SET_H
#define SET_H

#include <iostream>
using namespace std;

template<class ItemType>
class Set;


template <class ItemType>
class Set{
  private:
          ItemType *elements; // This is a sample representation
          int capacity; // You are free to use other representations
          int num_of_elements;
  public: 
          Set(); // Constructor
          Set(const Set<ItemType>& other); // Copy Constructor
          ~Set(); // Destructor

          /**Member Functions**/

          void  addElement (const ItemType &element);
          /* Add the element to the set. 
          If the element is already in the set DO NOT add it. */
};
#endif


#include <iostream>
#include "set.h"
using namespace std;


template <class ItemType>
Set<ItemType>::Set(){
        elements = new ItemType[20];
        num_of_elements = 0;
        capacity = 10;
} 

template <class ItemType>
Set<ItemType>::Set(const Set<ItemType>& other):
    elements(other->elements), num_of_elements(other->num_of_elements), capacity(other->capacity)
{}

template <class ItemType>
Set<ItemType>::~Set(){
delete[] elements;
}

template <class ItemType>
void Set<ItemType>::addElement (const ItemType &element){
        ItemType * temp = new ItemType [capacity];
        for( int i = 0; i < num_of_elements ; i ++ ) {
        temp[i] = elements[i];}

            delete [] elements;
            elements = temp;

            elements[num_of_elements] = element;
            num_of_elements++;
}

template <class ItemType>
bool Set<ItemType>::removeElement(const ItemType &item){
        for( int i = 0; i < _size; i++ ) {
        if( _items[i] == item )
        }
        int place = i;
        if( place == -1 )
        return false;

        elements[place]= elements[num_of_elements-1];
        num_of_elements --;
        return true;
}



#include <iostream>
#include <cstdlib>
#include "set.h"

using namespace std;

int main(){

    Set<int> s;
    int x=8;
    s.addElement(x);

    for (int i=0;i<5;i++)
        s.addElement(i+6);

    cout << "s: " << s << endl;




    system("pause");
    return 0;
}



This is the code that i write but  i'm getting the following error.

1>Main.obj : error LNK2019: unresolved external symbol "public: __thiscall Set<int>::Set<int>(void)" (??0?$Set@H@@QAE@XZ) referenced in function _main
1>Main.obj : error LNK2019: unresolved external symbol "public: __thiscall Set<int>::~Set<int>(void)" (??1?$Set@H@@QAE@XZ) referenced in function _main
1>Main.obj : error LNK2019: unresolved external symbol "public: void __thiscall Set<int>::addElement(int const &)" (?addElement@?$Set@H@@QAEXABH@Z) referenced in function _main
1>Main.obj : error LNK2019: unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Set<int> const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABV?$Set@H@@@Z) referenced in function _main

That's the code that i write it's a homework about templates but i'm getting some annoying errors.

i couldn't find the source of the error messages i think its about templates how can i solve these problems?

nonegravity
  • 15
  • 1
  • 1
  • 6
  • 1
    The same question every day :) Just do search on stackoverflow "LNK2019 template" – nogard May 14 '13 at 16:38
  • @nogard, Personally, I search for "[C++] templates header". I might as well bookmark this one. It's been more frequent lately. – chris May 14 '13 at 16:43

1 Answers1

2

Templated classes needs to be fully defined when they are instantiated, that includes all the functions. The common solution is to put all functions in the header file as well.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • is There a way to do it in cpp file ? – nonegravity May 14 '13 at 16:40
  • @nonegravity, If you include the cpp at the bottom of the header. You can't link to the implementation, though, so there's little point, really. – chris May 14 '13 at 16:45
  • 1
    @nonegravity: ... or explicitly instantiate your template class for all the supported types. You will not be able to use it with arbitrary types of course. – nogard May 14 '13 at 16:47
  • ok i think i get the point but couldn't solve it so i tried to get all functions into the header file. Now here is the error: 1>Main.obj : error LNK2019: unresolved external symbol "class std::basic_ostream > & __cdecl operator<<(class std::basic_ostream > &,class Set const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABV?$Set@H@@@Z) referenced in function _main – nonegravity May 14 '13 at 16:51
  • @nonegravity, Try this: http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix – chris May 14 '13 at 17:00