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:
- [Solved] error: invalid operands of types ‘const char [6]‘ and ‘const char [6]‘ to binary ‘operator+‘
- [Solved] Error: In the Function of ‘fmt::v8::detail::error_handler::on_error(char const*)’
- C++ Compile Error: error: invalid conversion from ‘void*‘ to ‘char*‘ [-fpermissive]
- Tensorflow C++:You must define TF_LIB_GTL_ALIGNED_CHAR_ARRAY for your compiler
- [Solved] ERROR_POLICY: attempt to perform an operation not allowed by the security policy `PDF‘ @ error/const
- [kubernetes] the pod instance of calico node always reports an error and restarts
- Keras Concatenate error: Layer concatenate_1 was called with an input that isn’t a symbolic tensor…
- The showdialog() method in thread/threading. Timer/task reported an error: “before ole can be called, the current thread must be set to single thread unit (STA) mode.”
- Sending ‘const NSString *’ to parameter of type ‘NSString *’ discards qualifiers [Solved]
- [Solved] TypeError: super(type, obj): obj must be an instance or subtype of type
- [Solved] sending const NSString * to parameter of type NSString * discards qualifiers
- [Solved] ECharts Console Error: `resize` should not be called during main process
- [Solved] Illegal access: this web application instance has been stopped already
- JSON parse error: Can not deserialize instance of java.lang.String out of START_OBJECT token
- Android: Can’t create handler inside thread that has not called Looper.prepare() [Solved]
- React Hook “useState“ is called in function “xxx“ which is neither a React function component or
- The pip installation package under Windows reports an error: Microsoft Visual C++ 9.0 is required Unable to find vcvarsall.bat
- [Solved] Mybatis:Cause: org.apache.ibatis.builder.BuilderException: Error creating document instance.
- [Solved] error: taking address of temporary array av_make_error_string((char[AV_ERROR_MAX_STRING_SI…
- VUE: Property or method “deleteFun“ is not defined on the instance but referenced during render. [How to Fix]