2013-10-14 62 views
1

我不能編譯......我不知道什麼是錯在這裏......這是發生的錯誤:指向一個模板對象

void MainThread::run() 
{ 
    Set<int>* p_test; p_test = new Set<int>; 
    p_test->add(new int(9)); 
    std::cout<<"The set is: {"; 
    for (int x = 0; x < p_test->size(); x++) 
     std::cout<< ", " << p_test->toString(x).toStdString().c_str(); 

    std::cout<<"}"; 

    std::cin.get(); 
}//test method 

錯誤消息給出如下:「未定義參考設置:: Set()「,它顯示在我嘗試使用我的班級的線上。我的類自己編譯...下面是文件「Set.h」。任何人有任何想法,我可以如何解決它?提前致謝。

#ifndef SET_H 
#define SET_H 

#include <functional> 
#include <QList> 
#include <QString> 
#include <type_traits> 
#include <exception> 
#include <iostream> 

template<typename T> 
class Set { 
public: 
    //constructors 
    Set(); 
    ~Set(){ delete []pType; } 

    //functions 
    const int & size(){ return m_size; } 

    void add(const T * singleton); 

    void empty(); 

    //operators 
    inline T& operator [](int index){ return pType[index]; } 

    template<class Y> 
    friend Set<Y> operator *(const Set<Y>& s1, const Set<Y>& s2);//intersection 
    template<class Y> 
    friend Set<Y> operator *(Set<Y>& s1, Set<Y>& s2); 
    template<class Y> 
    friend Set<Y> operator +(const Set& s1, const Set& s2);//union 
    template<class Y> 
    friend Set operator -(const Set& s1, const Set& s2);//relative complement 

    bool operator =(const Set& other) 
    { 
     delete []pType;//empty out the array 

     /** Gets operator **/ 
     int x = other.size(); 
     pType = new T[x]; 
     for (int y = 0; y < x; y++) 
      pType[y] = other[y]; 

     m_size = x; 

     return true; 
    } 

    bool operator ==(const Set & other) 
    { 
     if(other.size() != size()) 
      return false; 
     else 
     { 
      for (int x = 0; x < size(); x++) 
       if (!other.has(pType[x])) 
        return false; 
     }//end else 
     return true; 
    }//end equals operator 

    /*template<typename Type> 
    bool operator *= (const Set<Type> &lhs, const Set<Type> &rhs){ 
     //compile time statement (just to let people know) 
     static_assert(std::is_same<Type1, Type2>::value, "Types are not equal!"); 
     return std::is_same<Type1, Type2>::value; 
    }//operator for checking if two things are the same type */ 

    bool operator >(const Set &other) 
    { /** Superset **/ return false; } 
    \ 
    bool operator <(const Set *other) 
    { /** Subset **/ return false; } 

    Set& complement(); 
    bool isEmpty(){ return m_size == 0; } 
    bool has(T* element); 
    QString toString(int index); 

private: 
    T * pType; 
    T * m_Type; //save the variable type. 
    int m_size; 
}; 

#endif // SET_H 

我確實在一個單獨的文件中定義了一個構造函數。

Set<Y>::Set() 
{ 
    m_size = 0; 
    m_Type = new Y();//save a default value 
}//create an empty set 

或者我需要一種不同類型的構造函數嗎?

+0

無法找到定義(方法體)到作爲構造函數的Set :: Set。庫巴的答案有你的解決方案 –

回答

2

您的Set類的每種方法都必須在頭文件中定義爲。您的頭文件缺少Set::Set()的定義以及其他一些方法。

+0

你能舉個例子嗎? – allenh1

+0

你不能不明白我先說的話。讓我重複一遍:*你的'Set'類的每個方法必須在頭文件中定義**。你說你的構造函數是在一個單獨的文件中定義的。它不可能。它必須在標題中定義。我已經說過,鏈接器說,你可以走多遠:) –

1

由於您正在編寫模板類,因此必須在頭文件中定義clas的所有方法。如

template<typename T> 
    class Set { 
    Set() { } // <-- declaration and definition 
    }; 

這是因爲編譯器不編譯模板類/功能,直到發現在實際使用它的代碼的地方,那麼你的類「不自行編譯」。

要編譯模板類,編譯器會在同一個文件中查找聲明和定義。然後,編譯器將生成實際實現特定模板參數的函數的代碼。

因此,使用模板時,請將函數定義放在同一個文件中。如果你想創建一個專業化,那麼專門的功能不再是一個模板,你必須把它放在一個單獨的文件,除非你聲明它inline

對不起,如果你的頭在閱讀所有這些後感到痛苦......歡迎來到C++。