jeudi 27 février 2020

Operator overload for vector function c++

so I'm sure this question is super simple, I'm just quite new to programming and to C++ in general. So for my class was making a Vector with a class template. My professor has supplied the .h file and we have to write the integrated .cpp file. Heres the .h file:

#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[];
}

I know that my overload operator is way off and makes no sense, I just dont understand the syntax well enough to even know where to begin. Obvisouly, the operator should return the value of the aptr at whatever index was passed in through []. Any help or insight would be amazing!

Aucun commentaire:

Enregistrer un commentaire