指向模板对象的指针

Pointer to a template Object

本文关键字:指针 对象      更新时间:2023-10-16

我无法编译...我不知道这里出了什么问题...这是发生错误的地方:

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

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

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

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

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

这是因为编译器在实际使用它的代码中找到位置之前不会编译模板化类/函数,因此您的类"不会自行编译"。

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

因此,在使用模板时,请将函数定义放在同一个文件中。如果要创建专用化,则专用功能不再是模板,除非将其声明为inline,否则必须将其放在单独的文件中。

对不起,如果你在阅读所有这些后头疼......欢迎来到C++。