Я уверен, что этот вопрос очень прост, я просто новичок в программировании и в 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
по любому индексу, который был передан через []
.