这个代码怎么完善成对的呀
#include <iostream>
using namespace std;
class IndexError{};
template
class ARRAY
{
size_t m_size;
T *m_ptr;
public:
ARRAY(size_t size) : m_size(size)
{
m_ptr = new T[size];
memset(m_ptr, 0, size*sizeof(int));
}
~ARRAY()
{
delete[] m_ptr;
}
T& at(int index);
};
template <typename T>
::at(int index)
{
if(index<0||
)
{
IndexError();
}
return m_ptr[index];
}
int main()
{
ARRAY<int> a(50);
int i;
cin >> i;
{
for(int j=0;j<i;j++)
a.at(i) = j;
}
catch(IndexError e)
{
return 0;
}
return 0;
}
#include <iostream>
#include <cstring>
using namespace std;
class IndexError : public exception
{
public:
const char *what()
{
return "下标越界异常";
}
};
template <typename T>
class ARRAY
{
size_t m_size;
T *m_ptr;
public:
ARRAY(size_t size) : m_size(size)
{
m_ptr = new T[size];
memset(m_ptr, 0, size * sizeof(int));
}
~ARRAY()
{
delete[] m_ptr;
}
T &at(int index);
};
template <typename T>
T &ARRAY<T>::at(int index)
{
if (index < 0 || index >= m_size)
{
throw IndexError();
}
return m_ptr[index];
}
int main()
{
ARRAY<int> a(50);
int i;
cin >> i;
try
{
for (int j = 0; j < i; j++)
a.at(i) = j;
}
catch (IndexError &e)
{
cout << e.what() << endl;
return 0;
}
return 0;
}