std::out_of_range 使用 gcc 编译代码后不会打印异常字符串
std::out_of_range exception string won't print after compiling code with gcc
我正在尝试测试以下异常处理代码:
#include "Array_Template.h"
using namespace std;
int main()
{
Array<int> intArr1;
//attempt to use out-of-range subscript
try{
cout << "\nAttempt to assign 1000 to intArr1[6]" << endl;
intArr1[6] = 1000;
} //end try
catch (const out_of_range &ex){
cout << "An exception occurred: " << ex.what() << endl;
} //end catch
return 0;
} //end main
在我的数组 class 模板中:
#ifndef ARRAY_H
#define ARRAY_H
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <stdexcept>
//forward declarations of friend functions as specializations of template
template<typename T> class Array;
template<typename T>
std::istream &operator>>(std::istream &, Array<T> &);
template<typename T>
std::ostream &operator<<(std::ostream &, const Array<T> &);
//Array class template
template<typename T>
class Array
{
friend std::istream &operator>> <>(std::istream &, Array<T> &);
friend std::ostream &operator<< <>(std::ostream &, const Array<T> &);
public:
Array(int = 5); //default constructor of array size 5
Array(const Array<T> &); //copy constructor
~Array(); //destructor
int getSize() const; //return size of array
const Array<T> &operator=(const Array<T> &); //overloaded assignment operator
bool operator==(const Array<T> &) const; //overloaded equality operator
bool operator!=(const Array<T> &r) const //overloaded inequality operator, inline definition
{
return !(*this == r); //invokes Array<T>::operator==
}
T &operator[](int); //overloaded subscript operator for non-const objects
T operator[](int) const; //overloaded subscript for const objects
private:
int size; //pointer-based array size
T *arrPtr; //pointer to first element of array
}; //end class Array
template<typename T>
Array<T>::Array(int s)
:size(s > 0 ? s : 5), arrPtr(new T[size])
{
for (int i = 0; i < size; ++i)
arrPtr[i] = 0;
} //end Array constructor
template<typename T>
//ref return creates a modifiable lvalue
T& Array<T>::operator[](int index)
{
if (index < 0 || index >= size) //check if out of bounds
throw std::out_of_range::out_of_range("index out of range");
return arrPtr[index]; //ref return
} //end operator[]
template<typename T>
//const ref return creates an rvalue
T Array<T>::operator[](int index) const
{
if (index < 0 || index >= size) //check if out of bounds
throw std::out_of_range::out_of_range("index out of range");
return arrPtr[index]; //returns copy of this element
} //end operator[]
#endif
程序在Visual Studio编译执行正常,显示正确的一行错误信息:
但是当我用 gcc 编译并运行同一个程序时,我得到以下输出:
* Error in `./arrays_template': free(): invalid size: 0x0000000001426030 *
======= Backtrace: ========= /lib64/libc.so.6(+0x7364f)[0x7fd7c2e3d64f]
/lib64/libc.so.6(+0x78eae)[0x7fd7c2e42eae]
/lib64/libc.so.6(+0x79b87)[0x7fd7c2e43b87] ./arrays_template[0x4011a1]
./arrays_template[0x400fc7]
/lib64/libc.so.6(__libc_start_main+0xf5)[0x7fd7c2debbe5]
./arrays_template[0x400c29]
其后是冗长的内存映射跟踪。
有人可以解释这里究竟发生了什么以及如何解决它吗?通过修复,我的意思是如何获得与 catch 块中指定的相同的错误消息。
以下代码错误:
throw std::out_of_range::out_of_range("index out of range");
// ^^^^^^^^^^^^^^
应该改写为:
throw std::out_of_range("index out of range");
调用std::out_of_range
的constructor
还要确保修改 operator[](int) const
的 return 类型。
将这两个运算符中的语句从
更改为
throw std::out_of_range::out_of_range("index out of range");
至
throw std::out_of_range("index out of range");
我正在尝试测试以下异常处理代码:
#include "Array_Template.h"
using namespace std;
int main()
{
Array<int> intArr1;
//attempt to use out-of-range subscript
try{
cout << "\nAttempt to assign 1000 to intArr1[6]" << endl;
intArr1[6] = 1000;
} //end try
catch (const out_of_range &ex){
cout << "An exception occurred: " << ex.what() << endl;
} //end catch
return 0;
} //end main
在我的数组 class 模板中:
#ifndef ARRAY_H
#define ARRAY_H
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <stdexcept>
//forward declarations of friend functions as specializations of template
template<typename T> class Array;
template<typename T>
std::istream &operator>>(std::istream &, Array<T> &);
template<typename T>
std::ostream &operator<<(std::ostream &, const Array<T> &);
//Array class template
template<typename T>
class Array
{
friend std::istream &operator>> <>(std::istream &, Array<T> &);
friend std::ostream &operator<< <>(std::ostream &, const Array<T> &);
public:
Array(int = 5); //default constructor of array size 5
Array(const Array<T> &); //copy constructor
~Array(); //destructor
int getSize() const; //return size of array
const Array<T> &operator=(const Array<T> &); //overloaded assignment operator
bool operator==(const Array<T> &) const; //overloaded equality operator
bool operator!=(const Array<T> &r) const //overloaded inequality operator, inline definition
{
return !(*this == r); //invokes Array<T>::operator==
}
T &operator[](int); //overloaded subscript operator for non-const objects
T operator[](int) const; //overloaded subscript for const objects
private:
int size; //pointer-based array size
T *arrPtr; //pointer to first element of array
}; //end class Array
template<typename T>
Array<T>::Array(int s)
:size(s > 0 ? s : 5), arrPtr(new T[size])
{
for (int i = 0; i < size; ++i)
arrPtr[i] = 0;
} //end Array constructor
template<typename T>
//ref return creates a modifiable lvalue
T& Array<T>::operator[](int index)
{
if (index < 0 || index >= size) //check if out of bounds
throw std::out_of_range::out_of_range("index out of range");
return arrPtr[index]; //ref return
} //end operator[]
template<typename T>
//const ref return creates an rvalue
T Array<T>::operator[](int index) const
{
if (index < 0 || index >= size) //check if out of bounds
throw std::out_of_range::out_of_range("index out of range");
return arrPtr[index]; //returns copy of this element
} //end operator[]
#endif
程序在Visual Studio编译执行正常,显示正确的一行错误信息:
但是当我用 gcc 编译并运行同一个程序时,我得到以下输出:
* Error in `./arrays_template': free(): invalid size: 0x0000000001426030 * ======= Backtrace: ========= /lib64/libc.so.6(+0x7364f)[0x7fd7c2e3d64f] /lib64/libc.so.6(+0x78eae)[0x7fd7c2e42eae] /lib64/libc.so.6(+0x79b87)[0x7fd7c2e43b87] ./arrays_template[0x4011a1] ./arrays_template[0x400fc7] /lib64/libc.so.6(__libc_start_main+0xf5)[0x7fd7c2debbe5] ./arrays_template[0x400c29]
其后是冗长的内存映射跟踪。 有人可以解释这里究竟发生了什么以及如何解决它吗?通过修复,我的意思是如何获得与 catch 块中指定的相同的错误消息。
以下代码错误:
throw std::out_of_range::out_of_range("index out of range");
// ^^^^^^^^^^^^^^
应该改写为:
throw std::out_of_range("index out of range");
调用std::out_of_range
的constructor
还要确保修改 operator[](int) const
的 return 类型。
将这两个运算符中的语句从
更改为throw std::out_of_range::out_of_range("index out of range");
至
throw std::out_of_range("index out of range");