为返回一对的对创建模板类

Creating a template class for a pair that returns a pair

本文关键字:建模 创建 返回      更新时间:2023-10-16

代码不起作用,我不确定我在哪里出错了任何帮助。对于一个名为" 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},您最终会再进行一份额外的副本而不是动作,这对于诸如intfloat之类的原始类型而言并不是一件大事,但是如果您要创建一对大型对象(例如,两个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)
{}