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() 的引用”,它显示在我尝试使用我的类的行上。我的类自己编译...下面是文件“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

还是我需要不同类型的构造函数?

4

2 回答 2

2

类的每个方法都Set必须在头文件中定义。您的头文件缺少Set::Set(), 以及其他一些方法的定义。

于 2013-10-14T01:46:15.480 回答
1

由于您正在编写模板类,因此必须在头文件中定义类的所有方法。如

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

这是因为编译器不会编译模板类/函数,直到在代码中找到实际使用它的位置,所以你的类“不会自行编译”。

为了编译模板类,编译器在同一个文件中查找声明和定义。然后编译器将生成实际实现特定模板化参数的函数的代码。

因此,在使用模板时,请将您的函数定义放在同一个文件中。如果你想创建一个特化,那么特化函数不再是一个模板,你必须把它放在一个单独的文件中,除非你声明它inline

抱歉,如果您在阅读完所有这些内容后感到头疼...欢迎使用 C++。

于 2013-10-14T02:09:50.157 回答