首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >向量函数c++的运算符重载

向量函数c++的运算符重载
EN

Stack Overflow用户
提问于 2020-02-28 01:06:44
回答 1查看 76关注 0票数 1

我相信这个问题非常简单,我只是对编程和C++非常陌生。

对于我的类,我们使用类模板创建一个Vector。我的教授已经提供了.h文件,我们必须编写集成的.cpp文件。下面是.h文件:

代码语言:javascript
复制
#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

下面是我的实现:

代码语言:javascript
复制
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
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值。

EN

回答 1

Stack Overflow用户

发布于 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操作,例如:

代码语言:javascript
复制
template <class T>
T& SimpleVector<T>::operator[](const int sub)
{
    return aptr[sub];
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60438419

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档