C ++错误LNK2019:未解析的外部符号

时间:2013-05-14 16:34:59

标签: c++ templates

#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

这是我写的代码是关于模板的作业,但我遇到了一些烦人的错误。

我找不到错误消息的来源我认为它的模板如何解决这些问题?

1 个答案:

答案 0 :(得分:2)

模板化类在实例化时需要完全定义,包括所有函数。常见的解决方案是将所有函数放在头文件中。