c++ array[]操作符抛出不工作
c++ array [] operator throws not working
好,我为我的safeArray::操作符[]创建了一些抛出,但是这些抛出只对某些数字有效——其他数字由于某种原因导致程序崩溃。
现在如果我有:
if (index < startingIndex)
{
try
{
throw 1;
}
catch (int &e)
{
printf("caught itn"); throw;
}
}
抛出按预期工作,不会使程序崩溃,但是如果我包含:
if (index >= capacity)
{
try
{
throw 1;
}
catch (int &e)
{
printf("caught itn"); throw;
}
}
当我将这段代码添加到其他数字时,我的程序崩溃并说abort()已被调用。据我所知,参数是正确的,所以我不确定为什么它崩溃了。
剩下的代码:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <sstream>
//To prevent those using g++ from trying to use a library
//they don't have
#ifndef __GNUC__
#include <conio.h>
#endif
using namespace std;
class SafeArray {
public:
SafeArray();
SafeArray(const int, const unsigned int);
~SafeArray();
int& operator[](const int);
friend string arrayToString(const SafeArray&);
int getLowIndex();
int getHighIndex();
int length();
void resize(const int, const unsigned int);
private:
unsigned int capacity;
int * arr;
int startingIndex;
};
int SafeArray::getHighIndex(){
int low = this->startingIndex;
int high = this->capacity;
int retval = low + high - 1;
return retval;
}
int SafeArray::getLowIndex() {
return startingIndex;
}
int SafeArray::length() {
return capacity;
}
void SafeArray::resize(const int start, const unsigned int capacity1)
{
int delta = startingIndex - start;
size_t newSize = capacity1;
int* newArr = new int[newSize];
memcpy(newArr+delta, arr, capacity * sizeof(int));
capacity = newSize;
delete[] arr;
this->startingIndex = start;
this->capacity = capacity1;
arr = newArr;
}
//This helps with testing, do not modify.
bool checkTest(string testName, string whatItShouldBe, string whatItIs) {
if (whatItShouldBe == whatItIs) {
cout << "Passed " << testName << endl;
return true;
}
else {
cout << "****** Failed test " << testName << " ****** " << endl << " Object contained: " << whatItIs << endl << " Output should have contained: " << whatItShouldBe << endl;
return false;
}
}
//This helps with testing, do not modify.
bool checkTest(string testName, int whatItShouldBe, int whatItIs) {
if (whatItShouldBe == whatItIs) {
cout << "Passed " << testName << endl;
return true;
}
else {
cout << "****** Failed test " << testName << " ****** " << endl << " Object contained: " << whatItIs << endl << " Output should have contained: " << whatItShouldBe << endl;
return false;
}
}
string arrayToString(const SafeArray& obj) {
stringstream ss;
for (unsigned int i = 0; i < obj.capacity; i++) {
ss << obj.arr[i] << " ";
};
string str = ss.str();
size_t found;
found = str.find_last_not_of(" ");
if (found != string::npos)
str.erase(found + 1);
else
str.clear(); // str is all whitespace
return str;
}
SafeArray::SafeArray(const int startingIndex, const unsigned int capacity) {
this->arr = new int[capacity];
this->startingIndex = startingIndex;
this->capacity = capacity;
}
SafeArray::~SafeArray() {
delete[] arr;
}
int& SafeArray::operator[](int index) {
if (index < startingIndex)
{
try
{
throw 1;
}
catch (int &e)
{
printf("caught itn"); throw;
}
}
return arr[index-startingIndex];
}
void safeArrayTests() {
//Test constructor
SafeArray a(0, 5);
for (int i = 0; i < 5; i++) {
a[i] = i + 20;
};
checkTest("safeArrayTests #1", "20 21 22 23 24", arrayToString(a));
//Test methods
checkTest("safeArrayTests #2", 0, a.getLowIndex());
checkTest("safeArrayTests #3", 4, a.getHighIndex());
checkTest("safeArrayTests #4", 5, a.length());
//Test operator [] overload
checkTest("safeArrayTests #5", 20, a[0]);
checkTest("safeArrayTests #6", 24, a[4]);
try {
checkTest("safeArrayTests #7", 0, a[5]);
cout << "Passed safeArayTests #7" << endl;
}
catch (int error) {
cout << "Passed safeArayTests #7" << endl;
}
try {
checkTest("safeArrayTests #8", 0, a[-1]);
cout << "****** Failed test safeArayTests #8 ******. An error should have been thrown, but wasn't." << endl;
}
catch (int error) {
cout << "Passed safeArayTests #8" << endl;
}
//Test offsetting the array
SafeArray b(5, 10);
int i;
for (i = 5; i < 15; i++) {
b[i] = i + 100;
};
checkTest("safeArrayTests #9", "105 106 107 108 109 110 111 112 113 114", arrayToString(b));
//Test methods
checkTest("safeArrayTests #10", 5, b.getLowIndex());
checkTest("safeArrayTests #11", 14, b.getHighIndex());
checkTest("safeArrayTests #12", 10, b.length());
//Test operator [] overload
checkTest("safeArrayTests #13", 105, b[5]);
checkTest("safeArrayTests #14", 114, b[14]);
try {
checkTest("safeArrayTests #15", 0, b[4]);
cout << "****** Failed test safeArayTests #7 ******. An error should have been thrown, but wasn't." << endl;
}
catch (int error) {
cout << "Passed safeArayTests #15" << endl;
}
try {
checkTest("safeArrayTests #16", 0, a[15]);
cout << "****** Failed test safeArayTests #7 ******. An error should have been thrown, but wasn't." << endl;
}
catch (int error) {
cout << "Passed safeArayTests #16" << endl;
}
//Test resizing
//Make sure the existing elements were untouched.
b.resize(3, 15);
checkTest("safeArrayTests #17", 3, b.getLowIndex());
checkTest("safeArrayTests #18", 17, b.getHighIndex());
checkTest("safeArrayTests #19", 15, b.length());
checkTest("safeArrayTests #20", 105, b[5]);
checkTest("safeArrayTests #21", 114, b[14]);
//attempt to write to b[3] and b[17] without crashing
b[3] = 1;
b[17] = 2;
checkTest("safeArrayTests #22", 1, b[3]);
checkTest("safeArrayTests #23", 2, b[17]);
try {
b[2] = 666;
cout << "****** Failed test safeArayTests #24 ******. An error should have been thrown, but wasn't." << endl;
}
catch (int error) {
cout << "Passed safeArayTests #24" << endl;
}
//Test resizing into negative
b.resize(-4, 23);
checkTest("safeArrayTests #25", 105, b[5]);
checkTest("safeArrayTests #26", 114, b[14]);
checkTest("safeArrayTests #27", 1, b[3]);
checkTest("safeArrayTests #28", 2, b[17]);
b[-4] = 1234;
checkTest("safeArrayTests #29", 1234, b[-4]);
//Test offsetting the array
SafeArray c(-20, 4);
for (i = -20; i < -16; i++) {
c[i] = i - 10;
};
checkTest("safeArrayTests #30", "-30 -29 -28 -27", arrayToString(c));
//Test methods
checkTest("safeArrayTests #31", -20, c.getLowIndex());
checkTest("safeArrayTests #32", -17, c.getHighIndex());
checkTest("safeArrayTests #33", 4, c.length());
//Test operator [] overload
checkTest("safeArrayTests #34", -30, c[-20]);
checkTest("safeArrayTests #35", -27, c[-17]);
//Test resizing
//Make sure the existing elements were untouched.
c.resize(-21, 6);
checkTest("safeArrayTests #36", -21, c.getLowIndex());
checkTest("safeArrayTests #37", -16, c.getHighIndex());
checkTest("safeArrayTests #38", 6, c.length());
checkTest("safeArrayTests #39", -30, c[-20]);
checkTest("safeArrayTests #40", -27, c[-17]);
//attempt to write to c[-21] and c[-16] without crashing
c[-21] = 1;
c[-16] = 2;
checkTest("safeArrayTests #41", 1, c[-21]);
checkTest("safeArrayTests #42", 2, c[-16]);
//Test an array of one element
SafeArray d(3, 1);
d[3] = 1111;
checkTest("safeArrayTests #43", "1111", arrayToString(d));
//Test methods
checkTest("safeArrayTests #44", 3, d.getLowIndex());
checkTest("safeArrayTests #45", 3, d.getHighIndex());
checkTest("safeArrayTests #46", 1, d.length());
//Test operator [] overload
checkTest("safeArrayTests #47", 1111, d[3]);
//Test resizing
//Make sure the existing elements were untouched.
d.resize(2, 2);
checkTest("safeArrayTests #48", 2, d.getLowIndex());
checkTest("safeArrayTests #49", 3, d.getHighIndex());
checkTest("safeArrayTests #50", 2, d.length());
checkTest("safeArrayTests #51", 1111, d[3]);
//attempt to write to d[2] without crashing
d[2] = 2222;
checkTest("safeArrayTests #52", 2222, d[2]);
}
int main() {
safeArrayTests();
#ifndef __GNUC__
cout << "Press any key to continue" << endl;
_getch();
#endif
}
解决方案:解决方案:if (index> SafeArray::getHighIndex()) {throw 1;}
首先,你似乎有一个逻辑错误:
您希望支持自定义边界,因此if (index >= capacity)
是错误的,因为您需要考虑startingIndex
和capacity
以获得正确的上界。
接下来,我没有测试比较 int和unsigned int的效果,但是您可能希望在执行比较之前将capacity
强制转换为更大的有符号类型。
相关文章:
- QSqlquery prepare()和bindvalue()不工作
- 导入库可以跨dll版本工作吗
- 我怎样才能让getline操作符工作
- c++ array[]操作符抛出不工作
- 重载*操作符,使其在左右两边都工作
- c++操作符的内部工作原理
- 类中的操作符是如何工作的
- 解引用操作符(*)作为类的成员函数重载时是如何工作的?
- 操作符重载在没有引用的情况下可以工作吗?
- 如何使重载操作符与指针一起工作
- 空体的重载操作符神奇地工作了
- c和c++中sizeof操作符的工作方式不同吗?
- std::set的操作符重载不能正常工作
- 模板重载=操作符在一个类中工作,在另一个类中无法编译
- c++模板操作符+如何使不同的实例与它一起工作
- 这个优先级操作符是如何工作的
- 重载操作符在方法中不工作
- 为什么数组订阅操作符可以双向工作
- c++用户定义操作符<不能正常工作
- 字符串class ==操作符不能正常工作