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

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

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

4

1 回答 1

2

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

于 2013-05-14T16:38:16.533 回答