I have a C++ class and I am trying to run it in Ubuntu:
#ifndef WRONGPARAMETEREXCEPTION_H_ #define WRONGPARAMETEREXCEPTION_H_ #include <iostream> #include <exception> #include <string> using namespace std; #pragma once class WrongParameterException: public exception { public: WrongParameterException(char* message): exception(message) {}; virtual ~WrongParameterException() throw() {}; }; #endif
when I try to compile it, the compiler gives me this error:
WrongParameterException.h: In constructor ‘WrongParameterException::WrongParameterException(char*)’: WrongParameterException.h:14: error: no matching function for call to ‘std::exception::exception(char*&)’ /usr/include/c++/4.3/exception:59: note: candidates are: std::exception::exception() /usr/include/c++/4.3/exception:57: note: std::exception::exception(const std::exception&)
Can anyone tell me what am I doing wrong? I tried changing the message variable to string or const string or const string& but it didn’t help.
Here is how I use the new exception that I created from main:
try { if ((strToInt1 == -1) || (parameters[1] == NULL) || (strToInt3 == -1) || (parameters[3] != NULL)) { throw WrongParameterException('Error in the config or commands file'); } } catch(WrongParameterException e) { log.addMsg(e.what()); }
First,
#pragma onceis the wrong way to go about it, learn about header include guards. Related question on SO explains why using#pragma onceis the wrong way to go about it. Wikipedia explains how to use include guards which serve the same purpose without any of the downsides.Second, you are calling the constructor of std::exception with a parameter it does not know, in this case a pointer to a character array.
Would probably be what you want. For more information on exceptions, check out C++ FAQ Lite article on Exceptions and the exceptions article at cplusplus.com.
Good luck!