模板类错误

时间:2016-12-09 16:51:54

标签: c++ class templates

我必须创建一个模板类。该类创建一个集合,您可以在其中输入和显示数字。我一直在为架构x86_64编译错误获取未定义的符号。我真的找不到我的错误。有人能引起我的注意吗?

Set.h

#ifndef SET_H
#define SET_H
#include <iostream>

using namespace std;

template <class T>
class Set
{
    public:

        Set(T n = DEFAULT_SIZE);
        Set(T a[], T n);
        void populate ();
        bool isMember (T n);
        void insertEl (T n);
        void display()const;
        void expandSet(T n);
        ~Set();

    private:
        T * set;
        T pSize;
        T numElements;
        static const T DEFAULT_SIZE;
};

#endif // SET_H

Set.cpp

#include "Set.h"
#include <iostream>

using namespace std;

//***************************************************
// Default constructor to initialize the set to     *
// the empty set. In other words, a set that        *
// contains no elements.                            *
//***************************************************

template <class T>
Set<T>::Set(T n)
{
    cout << "Default constructor[Set(int n)] was called: " << endl;
    numElements = 0;
    set = new T[n];
    cout << "New set was created of size " << n << endl << endl;
    pSize = n;
}

//***************************************************
// Constructor to initialize the set from the array *
// of integers passed to the constructor. The       *
// arguments to this constructor are the array of   *
// integers, size of the passed array.              *
//***************************************************

template <class T>
Set<T>::Set(T a[], T n)
{
    cout << "Constructor [Set(int a[], int n)] was called: " << endl;
    numElements = 0;
    set = new T[n];
    for (int i = 0; i < n; i++)
    {
        set[i] = a[i];
        numElements++;
    }
    pSize = n;
    cout << "New set was created of size " << n << endl << endl;
}

//***************************************************
// A method to populate the set of integers from    *
// user input                                       *
//***************************************************

template <class T>
void Set<T>::populate ()
{
    T n;
    cout << "Enter the number of elements: ";
    cin >> n;
    if (n + numElements > pSize)
    {
        expandSet(n);
    }
    T num;

    cout << "Enter the elements, separating each by a space: ";
    for (int i = 0; i < n; i++)
    {
        cin >> num;
        if (!isMember(num))
        {
            set[numElements]= num;
            numElements++;
        }
    }

}
//****************************************************
// A method to expand the set by a value n           *
//****************************************************

template <class T>
void Set<T>::expandSet(T n)
{
    if (n + numElements > pSize)
    {
        pSize += n+pSize;
        cout<<"Expanding, pSize is now "<<pSize<<endl;
        int *temp = new T[pSize];
        for (int i=0; i< numElements ; i++)
            temp[i]=set[i];
        delete [] set;
        set = temp;
    }
}

//****************************************************
// A method to check if element n is already in the  *
// set. Returns true or false if not.
//****************************************************

template <class T>
bool Set<T>::isMember (T n)
{
    for (int i = 0; i < numElements; i++)
    {
        if (set[i] == n)
            return true;
    }
    return false;
}

template <class T>
void Set<T>::insertEl (T n)
{
    if (!isMember(n))
    {
        if (numElements >= pSize)
        {
            expandSet(pSize);
        }
        set[numElements] = n;
        numElements++;
    }
}

//****************************************************
// A method to display the set. Note that the set    *
// should be displayed using proper set notation     *
//****************************************************

template <class T>
void Set<T>::display()const
{
    for (int i = 0; i < numElements; i++)
    {
        if (i == 0)
            cout << "{";
        cout << set[i];
        if (i == numElements - 1)
            cout << "}";
        else
            cout << ", ";
    }
    cout << endl;
}

template <class T>
Set<T>::~Set()
{
    delete [] set;
}

template <class T>
const T Set<T>::DEFAULT_SIZE = 5;

的main.cpp

#include <iostream>
#include "Set.h"

using namespace std;

int main()
{
    Set<int>set1;
    return 0;
}

我的编译错误:

Undefined symbols for architecture x86_64:
  "Set<int>::DEFAULT_SIZE", referenced from:
      _main in main.o
  "Set<int>::populate()", referenced from:
      _main in main.o
  "Set<int>::Set(int)", referenced from:
      _main in main.o
  "Set<int>::~Set()", referenced from:
      _main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

0 个答案:

没有答案