C++ 异常处理和模板
C++ exception handling and template
我是 C++ 的新手,这个关于异常处理和模板的作业给我带来了问题。我不明白为什么我的 catch 不工作。我收到“错误:‘)’标记前的预期不合格 ID。有人可以纠正这个问题并解释为什么我会收到这个错误吗?
感谢所有能指导我的人。
编辑:根据 Tony D 的评论,catch 和编译器可以工作,但不会打印 catch 消息。异常 class Error 没有像我编码的那样工作。关于如何修复 class?
的任何提示
这是我的程序:
// This program demonstrates an overloaded [] operator.
#include <iostream>
#include <cstdlib>
using namespace std;
class IntArray
{
private:
int *aptr;
int arraySize;
public:
IntArray(int); // Constructor
IntArray(const IntArray &); // Copy constructor
~IntArray(); // Destructor
int size() const { return arraySize; }
void subError() const; // Handles subscripts out of range
int &operator[](int) const; // Overloaded [] operator
};
class Error
{
public:
int value;
Error(int i)
{
value=i;
}
};
IntArray::IntArray(int s)
{
arraySize = s;
aptr = new int [s];
for (int count = 0; count < arraySize; count++)
*(aptr + count) = 0;
}
IntArray::IntArray(const IntArray &obj)
{
arraySize = obj.arraySize;
aptr = new int [arraySize];
for(int count = 0; count < arraySize; count++)
*(aptr + count) = *(obj.aptr + count);
}
IntArray::~IntArray()
{
if (arraySize > 0)
{
delete [] aptr;
arraySize = 0;
aptr = NULL;
}
}
void IntArray::subError() const
{
cout << "ERROR: Subscript out of range.\n";
exit(0);
}
int &IntArray::operator[](int sub) const
{
if (sub < 0 || sub >= arraySize)
throw Error(*aptr);
return aptr[sub];
}
int main()
{
const int SIZE = 10; // Array size
// Define an IntArray with 10 elements.
IntArray table(SIZE);
try
{
// Store values in the array.
for (int x = 0; x < SIZE; x++)
{
table[x] = (x * 2);
}
// Display the values in the array.
for (int x = 0; x < SIZE; x++)
{
cout << table[x] << " ";
}
cout << endl;
// Try to print an element out of bounds
cout << table[-1] << endl;
}
catch(const Error&)
{
table.subError();
}
return 0;
}
I can't figure out why my catch isn't working.
你的 Error
class 没有嵌套在你的 IntArray
class 里面,所以改变...
catch(const IntArray::Error)
...到...
catch(const Error&)
我是 C++ 的新手,这个关于异常处理和模板的作业给我带来了问题。我不明白为什么我的 catch 不工作。我收到“错误:‘)’标记前的预期不合格 ID。有人可以纠正这个问题并解释为什么我会收到这个错误吗?
感谢所有能指导我的人。
编辑:根据 Tony D 的评论,catch 和编译器可以工作,但不会打印 catch 消息。异常 class Error 没有像我编码的那样工作。关于如何修复 class?
的任何提示这是我的程序:
// This program demonstrates an overloaded [] operator.
#include <iostream>
#include <cstdlib>
using namespace std;
class IntArray
{
private:
int *aptr;
int arraySize;
public:
IntArray(int); // Constructor
IntArray(const IntArray &); // Copy constructor
~IntArray(); // Destructor
int size() const { return arraySize; }
void subError() const; // Handles subscripts out of range
int &operator[](int) const; // Overloaded [] operator
};
class Error
{
public:
int value;
Error(int i)
{
value=i;
}
};
IntArray::IntArray(int s)
{
arraySize = s;
aptr = new int [s];
for (int count = 0; count < arraySize; count++)
*(aptr + count) = 0;
}
IntArray::IntArray(const IntArray &obj)
{
arraySize = obj.arraySize;
aptr = new int [arraySize];
for(int count = 0; count < arraySize; count++)
*(aptr + count) = *(obj.aptr + count);
}
IntArray::~IntArray()
{
if (arraySize > 0)
{
delete [] aptr;
arraySize = 0;
aptr = NULL;
}
}
void IntArray::subError() const
{
cout << "ERROR: Subscript out of range.\n";
exit(0);
}
int &IntArray::operator[](int sub) const
{
if (sub < 0 || sub >= arraySize)
throw Error(*aptr);
return aptr[sub];
}
int main()
{
const int SIZE = 10; // Array size
// Define an IntArray with 10 elements.
IntArray table(SIZE);
try
{
// Store values in the array.
for (int x = 0; x < SIZE; x++)
{
table[x] = (x * 2);
}
// Display the values in the array.
for (int x = 0; x < SIZE; x++)
{
cout << table[x] << " ";
}
cout << endl;
// Try to print an element out of bounds
cout << table[-1] << endl;
}
catch(const Error&)
{
table.subError();
}
return 0;
}
I can't figure out why my catch isn't working.
你的 Error
class 没有嵌套在你的 IntArray
class 里面,所以改变...
catch(const IntArray::Error)
...到...
catch(const Error&)