首页 > 解决方案 > 向量函数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通过 传入的任何索引处的值[]

标签: c++c++11

解决方案


您的代码存在多个问题,包括:

  • 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];
}

推荐阅读