c++ - 向量函数c ++的运算符重载
问题描述
我确信这个问题非常简单,我对编程和一般的 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
通过 传入的任何索引处的值[]
。
解决方案
您的代码存在多个问题,包括:
未
bad_alloc
正确捕获(永远不要按值捕获异常类,始终按引用)。的重复定义
memError()
。您需要删除其中之一。~SimpleVector()
使用delete
而不是delete[]
.SimpleVector(const SimpleVector &)
在需要进行深拷贝时进行浅拷贝。sv.aptr
getElementAt()
subError()
如果sub
超出范围则不调用。从历史上看,operator[]
不执行范围检查,但如果您愿意/需要,您可以执行。
但是,要回答您提出的具体问题,您operator[]
的实施完全错误。除了在编写其定义时明显的语法错误以及定义与声明不匹配之外,在内部它只需要执行与该方法正在执行的相同return
操作,例如:getElementAt()
template <class T>
T& SimpleVector<T>::operator[](const int sub)
{
return aptr[sub];
}
推荐阅读
- sql - 如何使用 ST_Touches 获取触摸几何的相交长度
- shell - 如何在 zsh 脚本中编写复杂的 psql 命令?
- c# - 使用 IronWebScraper 时如果一个 URL 返回 404,如何继续抓取下一个 URL
- python - 使用现有变量引用 pandas 列名
- functional-programming - 带有延续问题的 F# 扩展欧几里得算法
- r - 在 R 中生成随机块示意图
- angular - 如何获取
标记动态值并使用 Angular 2 将其传递给打字稿
- python - 将鼠标光标移动到从文本文件导入的坐标
- reactjs - ReactJs MaterialUi onRowUpdate 字段验证
- redirect - IIS 重定向规则将一个 url 重定向到另一个