Terminate call after calling instance 'char const *' error

I'm new to programming, and when I try to use exception handling, I got an error in code block 16:01

ending a call after calling the instance of 'char const *'

this is mistake.

can someone help me with this error, I tried to reset the default IDE but did not work

code

#include <iostream> #include <cmath> #include <stdexcept> using namespace std; double sqrt(double num) { if(num < 0) throw "Negative number is not allowed"; double x = pow(num,0.5); return x; } int main() { double x; cout <<"Enter a number : "; cin >> x; double num; try { num = sqrt(x); } catch(const char *text) { cout << "ERROR : "<<text<<endl; return 1; } cout <<"Square root of "<< num <<" is : "<<num; return 0; } 
+5
source share
1 answer

Regardless of the implementation details that led to the error, your program has undefined behavior because you are using the reserved function signature from the C library.

http://eel.is/c++draft/reserved.names#2

If a program declares or defines a name in the context where it is reserved, except as expressly permitted by this clause, its behavior is undefined.

http://eel.is/c++draft/reserved.names#extern.names-4

Each function signature from the C standard library declared using an external link is reserved for implementation to be used as a functional signature with extern "C" and extern "C++" , or as the namespace namespace in the global namespace.

In your particular instance, it looks like your compiler library defines sqrt as noexcept , but ends up using a definition that you provide that does the throw, causing terminate called.

+2
source

Source: https://habr.com/ru/post/1276118/


All Articles