C++;异常处理和模板 我是C++新手,有关于异常处理和模板的任务,这给了我一些问题。我弄不明白为什么我的捕获物不起作用。我得到了“错误:在“)”之前需要非限定id”标记。有人能纠正这个问题并解释我为什么会得到这个错误吗
感谢所有能指引我的人 编辑:根据Tony D的评论,catch和编译器可以工作,但没有打印catch消息。异常类Error没有按照我编写的那样工作。有关于如何修复该类的提示吗 这是我的节目:C++;异常处理和模板 我是C++新手,有关于异常处理和模板的任务,这给了我一些问题。我弄不明白为什么我的捕获物不起作用。我得到了“错误:在“)”之前需要非限定id”标记。有人能纠正这个问题并解释我为什么会得到这个错误吗,c++,try-catch,C++,Try Catch,感谢所有能指引我的人 编辑:根据Tony D的评论,catch和编译器可以工作,但没有打印catch消息。异常类Error没有按照我编写的那样工作。有关于如何修复该类的提示吗 这是我的节目: // This program demonstrates an overloaded [] operator. #include <iostream> #include <cstdlib> using namespace std; class IntArray { priv
// 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;
}
//此程序演示重载的[]运算符。
#包括
#包括
使用名称空间std;
类阵列
{
私人:
int*aptr;
内部阵列化;
公众:
IntArray(int);//构造函数
IntArray(const IntArray&);//复制构造函数
~IntArray();//析构函数
int size()常量{return arraySize;}
void subError()const;//处理超出范围的下标
int&运算符[](int)const;//重载的[]运算符
};
类错误
{
公众:
int值;
错误(int i)
{
值=i;
}
};
IntArray::IntArray(ints)
{
arraySize=s;
aptr=新整数[s];
for(int count=0;count0)
{
删除[]aptr;
arraySize=0;
aptr=NULL;
}
}
数组中的void::subError()常量
{
cout=阵列化)
抛出错误(*aptr);
返回aptr[sub];
}
int main()
{
常量int SIZE=10;//数组大小
//定义一个包含10个元素的阵列。
阵列表(尺寸);
尝试
{
//在数组中存储值。
用于(int x=0;x
您的Error
类未嵌套在IntArray
类中,因此请更改
catch(const IntArray::Error)
…到
catch(const Error&)
很好,您至少尝试编写了catch
语句,但是对于min
模板…听起来您甚至还没有尝试过任何东西…?这是可行的,但是我应该在Error函数中做些什么更改才能使抛出有效?@TheJons the throw应该已经对“work”的某些定义“起作用”“-它应该编译并到达catch块,但是1)您可能希望抛出错误(sub):
这样它记录无效索引,而捕获代码可以显示它(您当前抛出数组中aptr
跟踪的第一个数字-这似乎是任意的和无关的),2)一旦捕获到异常,您就不想非常感谢您的提示Tony D。我一直忽略我想尝试代码的事实,并将表[-1]放在catch块中很长一段时间。