Reputation: 129
I have been several post with something similar to my problem but I can't figure out how to solve it in my implementation.
It's about a custom exception classes that compile perfectly in Linux with gcc 9, but in MacOs (Catalina) with Clang 11.0.3 fails dramatically.
Here is the code:
#include <stdexcept>
class Exception : public std::runtime_error
{
public:
Exception() : std::runtime_error("mt library error") {}
Exception(const std::string& what_arg) : std::runtime_error(what_arg) {}
};
/// Division by zero exception.
class ZeroDivide : public Exception
{
public:
ZeroDivide() : Exception("Division by near-zero value") {}
};
When compile in MacOS I get the error:
error: no matching constructor for initialization of 'mt::Exception'
ZeroDivide() : Exception("Division by near-zero value") {}
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
and different notes like:
candidate constructor (the implicit copy constructor) not viable: no known conversion from 'const char [36]' to 'const mt::Exception' for 1st argument
and other errors derived from the first one when I try to throw the exception like:
Exception("Normalizing interval is ill defined")
I have seen some examples like this of how could fix the problem but I can not figure out how to reimplement mine in order to make it work in MacOs
Somebody can help me?
Thanks in advance.
Edit
As the project is big, I have created a repository here with a minimal example to reproduce the error, you just have to compile following this steps
mkdir build
cd build
cmake ..
make
Upvotes: 1
Views: 1453
Reputation: 3030
It looks like your compiler is complaining that it won't automatically convert the hardcoded string "Division by near-zero value"
that you provide to the constructor of Exception
from const char*
to const std::string&
.
I have no access to macos at the moment but my guess is that you could fix it by constructing a std::string
at the place of calling the base constructor:
ZeroDivide() : Exception(std::string("Division by near-zero value")) {}
Another possible fix is to define a constructor in Exception
that takes a const char*
:
Exception(const char* what_arg) : std::runtime_error(what_arg) {}
This might be the better fix, as std::runtime_error
actually has both constructors for const char*
and for const std::string&
.
Upvotes: 2