我相信这个问题非常简单,我只是对编程和C++非常陌生。
对于我的类,我们使用类模板创建一个Vector。我的教授已经提供了.h文件,我们必须编写集成的.cpp文件。下面是.h文件:
#ifndef SIMPLEVECTOR_H
#define SIMPLEVECTOR_H
#include <iostream>
#include <new> // Needed for bad-alloc exception
#include <cstdlib> // Needed for the exit function
using namespace std;
template <class T>
class SimpleVector
{
private:
T *aptr; // To point to the allocated array
int arraysize; // Number of elements in the array
void memError(); // Handles memory allocation errors
void subError(); // Handles subscripts out of range
public:
SimpleVector()
{
aptr = 0;
arraysize = 0;
}
SimpleVector(int s);
SimpleVector(const SimpleVector & sv);
~SimpleVector();
int size() const
{
return arraysize;
}
T getElementAt(int sub);
T &operator[](const int);
};
#endif //SIMPLEVECTOR_H下面是我的实现:
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
SimpleVector<T>::SimpleVector(int s)
{
if(s<1)
{
arraysize=1;
}
else
{
arraysize=s;
}
try
{
aptr = new T [arraysize];
}
catch (bad_alloc)
{
memError();
}
for(int i=0;i<arraysize;i++)
{
aptr[i]=0;
}
}
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
void SimpleVector<T>::memError()
{
cout<<"Error: cannot allocate memory."<<endl;
exit(EXIT_FAILURE);
}
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
void SimpleVector<T>::memError()
{
cout<<"Error: cannot allocate memory."<<endl;
exit(EXIT_FAILURE);
}
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
T SimpleVector<T>::getElementAt(int sub)
{
return aptr[sub];
}
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
SimpleVector<T>::~SimpleVector()
{
delete aptr;
}
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
void SimpleVector<T>::subError()
{
cout<<"Subscripts out of range."<<endl;
exit(EXIT_FAILURE);
}
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
SimpleVector<T>::SimpleVector(const SimpleVector & sv)
{
aptr=sv.aptr;
}
template <class T>
T<T>&::operator[](const int &)
{
return aptr[];
}我知道我的重载操作符是错误的,没有任何意义,我只是不能很好地理解语法,甚至不知道从哪里开始。显然,操作符应返回通过[]传入的任何索引处的aptr值。
发布于 2020-02-28 02:08:32
您的代码存在多个问题,包括:
bad_alloc (从不通过值捕获异常类,总是通过引用捕获异常类)。memError()的定义重复。您需要使用delete而不是delete[]删除其中一个them.~SimpleVector()。当需要制作深层副本时,SimpleVector(const SimpleVector &)会制作sv.aptr的浅层副本。如果sub超出范围,则getElementAt()不调用subError()。从历史上看,operator[]不执行范围检查,但是如果您愿意/需要的话,您可以执行。但是,为了回答您提出的具体问题,您的operator[]实现完全错误。除了在编写其定义时明显的语法错误,以及定义与声明不匹配之外,在内部它只需要执行与getElementAt()方法相同的return操作,例如:
template <class T>
T& SimpleVector<T>::operator[](const int sub)
{
return aptr[sub];
}https://stackoverflow.com/questions/60438419
复制相似问题