I am using "g++ (GCC) 4.7.2 20120921 (Red Hat 4.7.2-2)". The problem is this
1 2 3 4 5 6 7 8 9 10 11
main.cpp: In function ‘int main()’:
main.cpp:6:37: error: no matching function for call to ‘std::exception::exception(constchar [18])’
main.cpp:6:37: note: candidates are:
In file included from /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/ios:40:0,
from /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/ostream:40,
from /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/iostream:40,
from main.cpp:1:
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/exception:65:5: note: std::exception::exception()
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/exception:65:5: note: candidate expects 0 arguments, 1 provided
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/exception:62:9: note: std::exception::exception(const std::exception&)
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/exception:62:9: note: no known conversion for argument 1 from ‘constchar [18]’ to ‘const std::exception&’
The full program is
1 2 3 4 5 6 7 8 9 10
#include <iostream>
#include <exception>
usingnamespace std;
int main()
{
exception ex("Content of what()"); //I can NOT do this
cout<<ex.what();
return 0;
}
I tried also to #include <exception> but it wont help.
You can use something other than exception. Try something that extends exception? http://www.cplusplus.com/reference/exception/
I notice logic_error, runtime_error, bad_alloc, etc.
These classes extend the base class of exception and define the virtual what method.
Just to resolve your issue, the problem is that std::exception is a base class, with virtual functions. You should never instantiate an exception directly. Either derive your own class(es) from it and use those, or use one of the standard exception classes.
Personal nit-pick -- I think you should probably be throwing a std::runtime_error.