我不能編譯......我不知道什麼是錯在這裏......這是發生的錯誤:指向一個模板對象
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
或者我需要一種不同類型的構造函數嗎?
無法找到定義(方法體)到作爲構造函數的Set :: Set。庫巴的答案有你的解決方案 –