0. Error Codes:
//insert template <class DataType> void SeqList<DataType> :: Insert(int i, DataType x) { if (i >= MaxSize) throw "up"; if (i < 1 || i > length + 1) throw "The postions is not right"; for (int j = length; j >= i; j--) data[j] = data[j - 1]; //Note that the jth element exists at the j-1 subscript of the array data[i - 1] = x; length++; } //main function int main(){ int r[5]={1, 2, 3, 4, 5}; SeqList<int> L(r, 5); cout<<"The data after performing the insert operation is:"<<endl; L.PrintList( ); //output all elements try{ // L.Insert(2,10); L.Insert(10,30); }catch(char *e){ cout<<e<<endl; } cout<<"The data after performing the insert operation is:"<<endl; L.PrintList( ); //output all elements
In fact, the code is still relatively easy, it is an insertion problem in an array, in an inserted array in the wrong position, it will report an exception to handle. But in this program the exception is not caught, but the program will stop for a long time in the middle and show terminate called after throwing an instance of ‘char const*’, the reason for this situation is that the exception does not match up in the catch, the C++ destructor throws The exception will automatically call terminate() to terminate the program. Then how to solve this situation?
1. Problem-solving
In fact, it is very simple to solve the problem, just add const in front of char *e in catch, here char *e is turned into a string constant pointer.
int main(){ int r[5]={1, 2, 3, 4, 5}; SeqList<int> L(r, 5); cout<<"The data before performing the insert operation is:"<<endl; L.PrintList( ); //output all elements try{ // L.Insert(2,10); // The reason for the problem is that the exception type does not match and c++ starts its own exception handling // Solution: just add const in front of char to make the caught exception variable a constant L.Insert(10,30); // the second parameter 30 will cause an exception to be thrown (i > length + 1) }catch(const char *e){ cout<<e<<endl; } cout<<"After performing the insert operation the data is:"<<endl; L.PrintList( ); //output all elements }
Read More:
- VTK GetScalarPointer() and GetScalarComponentAsFloat() not work
- [Solved] Opencv Error: Error: Assertion failed (data) in cv::Mat::at, file … mat.inl.hpp, line 897(Accessed pixels of non-existent matrix)
- [Solved] C++ reason ncnn model error: Segmentation fault (core dumped)
- [Solved] Compile Error: undefined reference to `google::FlagRegisterer::FlagRegisterer
- No match for ‘operator =’ both ends of the equal sign do not match
- C++ Compile Error: error: invalid conversion from ‘void*‘ to ‘char*‘ [-fpermissive]
- C++ Primer Program in VsCode error: no match for call to ‘(std::__cxx11::string…)
- Vue: How to Solve Error uncaught (in promise) cancel
- After SpringBoot starts, exit the console directly and display Process finished with exit code 1
- Arrays must all be same length [How to Solve]
- Error: array bound is not an integer constant before ‘]’ token
- [Solved] Build Error Ineffective mark-compacts near heap limit Allocation failed – JavaScript heap out of memory
- [Solved] C + + compile Error: error: default argument given for parameter 3
- [Solved] TFrecords Create Datas Error: Number of int64 values != expected. Values size: 1 but output shape: [3]
- [Solved] PCL Compile Error: undefined reference to `pcl::PCLBase<pcl::PointXYZRGBA>:: XXX
- C++ BUG: [Error] invalid array assignment
- How to Solve OpenCV CVUI Error: LINK2019
- [HBase Error]“java.lang.OutOfMemoryError: Requested array size exceeds VM limit”
- [Solved] ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
- [Solved] FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed – JavaScript heap out of me