为返回一对的对创建模板类
Creating a template class for a pair that returns a pair
代码不起作用,我不确定我在哪里出错了任何帮助。对于一个名为" Pair"的课程。此类将代表在模板定义中参数化的类型的一对数据成员。例如,您可以拥有一对整数,一对双打等等。
/* so I'm trying to implement this driver this is the driver.cpp file and
* I'm trying to do it with a template class */
int main()
{
Pair<char> letters('a', 'd');
cout << "nThe first letter is: " << letters.getFirst();
cout << "nThe second letter is: " << letters.getSecond();
cout << endl;
cin.get();
}
//this is my .h file code
template <class T>
class Pair
{
private:
T first;
T second;
public:
Pair(const T, const T);
T getFirst();
T getSecond();
};
//this is my Pair.cpp
#include "Pair.h"
template<class T>
Pair<T>::Pair(const T first, const T second)
{
return first, second;
}
template<class T>
inline T Pair<T>::getFirst()
{
return first;
}
template<class T>
inline T Pair<T>::getSecond()
{
return second;
}
您的代码有两个不同的问题。
首先,您仅在 Pair.h
中对成员函数的声明和单独的 Pair.cpp
文件中的定义声明。尽管这适用于常规功能,但它不适合模板(请参阅为什么在标题文件中实现模板?(
第二个问题是您的构造函数不初始化Pair
类的数据成员,而是返回您传递给它的第二个参数,即使构造函数不能具有返回值。
要解决此问题,您需要将构造函数的定义更改为
template <typename T>
Pair<T>::Pair(T fst, T snd)
: first{std::move(fst)},
second{std::move(snd)} {
}
在这里不需要对std::move
的调用,但这是将值移至变量的惯用方式。如果您只做first{fst}
,您最终会再进行一份额外的副本而不是动作,这对于诸如int
或float
之类的原始类型而言并不是一件大事,但是如果您要创建一对大型对象(例如,两个1000个元素向量(然后这可以产生巨大的差异。
应用了这两个更改和添加后的包括警卫后,您应该最终使用一个Pair.h
文件
#ifndef PAIR_H
#define PAIR_H
template <class T>
class Pair
{
private:
T first;
T second;
public:
Pair(T, T);
T getFirst();
T getSecond();
};
template<class T>
Pair<T>::Pair(T fst, T snd)
: first{std::move(fst)},
second{std::move(snd)}
{
}
template<class T>
T Pair<T>::getFirst()
{
return first;
}
template<class T>
T Pair<T>::getSecond()
{
return second;
}
#endif
这是简单的一对。h模板
// Pair.h
// template for a pair of items of arbitrary types
#include <sstream>
using namespace std;
template <typename S, typename T>
class Pair
{
public:
Pair();
Pair(S,T);
Pair(Pair &);
~Pair();
const Pair &operator=(const Pair &other);
string toString();
S* getFirst();
T* getSecond();
int getPairCount();
void setFirst(S);
void setSecond(T);
private:
S *f;
T *s;
static int count;
};
template <typename S, typename T>
int Pair<S,T>::count=0;
// 0-parameter constructor
template <typename S, typename T>
Pair<S,T>::Pair()
{
f = NULL;
s = NULL;
count++;
}
// 2-param constructor
template <typename S, typename T>
Pair<S,T>::Pair(S x, T y)
{
f = new S; *f = x;
s = new T; *s = y;
count++;
}
// get first element in pointer
template <typename S, typename T>
S* Pair<S,T>::getFirst()
{
if (f!=NULL)
{ S *tmp = new S;
*tmp = *f;
return tmp;
}
else
return NULL;
}
// get second element in pointer
template <typename S, typename T>
T* Pair<S,T>::getSecond()
{
if (s!=NULL)
{
T *tmp = new T;
*tmp = *s;
return tmp;
}
else
return NULL;
}
// set first element
template <typename S, typename T>
void Pair<S,T>::setFirst(S x)
{
if (f==NULL)
f = new S;
*f = x;
}
// set second element
template <typename S, typename T>
void Pair<S,T>::setSecond(T y)
{
if (s==NULL)
s = new T;
*s = y;
}
// make a string representation
template <typename S, typename T>
string Pair<S,T>::toString()
{
stringstream ss;
ss<<"(";
if (f==NULL)
ss<<"NULL";
else
ss<<(*f);
ss<<",";
if (s==NULL)
ss<<"NULL";
else
ss<<(*s);
ss<<")";
return ss.str();
}
// keep count
template <typename S, typename T>
int Pair<S,T>::getPairCount(){
return count;}
//copy constructor
template <typename S, typename T>
Pair<S,T>::Pair(Pair &other)
{
f = NULL; s = NULL;
if(other.f != NULL)
f = new S(*other.f);
if(other.s != NULL)
s = new T(*other.s);
count++;
}
//destructor
template <typename S, typename T>
Pair<S,T>::~Pair()
{
if(f != NULL)
delete f;
if(s != NULL)
delete s;
f = NULL;
s = NULL;
count--;
}
//deep assignment
template <typename S, typename T>
const Pair<S,T> & Pair<S,T>::operator=(const Pair<S,T> &other)
{
if(this != &other)
{
if(f != NULL)
delete f;
if(s != NULL)
delete s;
f = NULL; s = NULL;
if(other.f != NULL)
f = new S(*other.f);
if(other.s != NULL)
s = new T(*other.s);
}
return *this;
}
构造函数不应返回任何内容。它的函数是通过传递的变量作为参数初始化对象。
template<class T>
Pair<T>::Pair(const T fst, const T scd) : first(fst), second(scd)
{}
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 使用std::multimap迭代器创建std::list
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- 使用CMake创建QML插件
- 如何在c++中为模板函数实例创建快捷方式
- 在C++中,是否可以基于给定的标识符创建基类的新实例,反之亦然
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- OpenCV EqualizeHist()从彩色图像创建黑白图像
- 试图在visual studio上用C++创建一个桌面应用程序
- std::threads可以从Windows DLL中的全局变量创建/销毁吗?
- 如何在C++20中创建模板别名的推导指南
- 如何为模板化对象创建模板向量?VS正在投掷C3203
- 如何创建一个空的全局类并在启动时实例化它
- 无法创建抽象类的实例
- 链接到自行创建的dll失败
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 如何在C++类内存结构中创建"spacer"?
- 终端不会为C++文件创建.exe文件吗
- 在createdialog创建的窗口中捕获用于编辑控件的OnMouseMove消息