指向模板对象的指针
Pointer to a template Object
我无法编译...我不知道这里出了什么问题...这是发生错误的地方:
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++。
相关文章:
- 为什么指针对象没有调用默认构造函数
- 如何访问双指针对象中的方法
- 如何使用智能指针对象执行成员函数指针
- 调用带有指针对象错误的指针变量
- 如何从模板类重载创建的指针对象上的运算符?
- C++指针对象的运算符重载
- 如何初始化数组指针对象
- 如何打印出唯一指针对象向量中的值(基于范围的循环)?C++
- C++指针对象差异
- 涉及继承,指针对象的交易以及何时进行的问题
- 表达式必须在C 中具有指针对象类型
- 下标需要数组或指针类型表达式必须具有指针对象类型
- 字符指针对象和相应的字符数组元素比较
- 如何在 c++ 中克隆为弱指针对象
- 指针对象使用CPP中的指针表达式进行比较
- Visual Studio中智能指针对象的自定义视图
- 使用指针 /对象 /模板参数正确构成
- 如果我确定只有一个线程一次处理指针/对象,则C/C 仍应使用同步
- 如何将值分配给指针对象 c++
- 更改智能指针对象,然后访问它