gpt4 book ai didi

C++ 错误 : no matching constructor for initialization of "my:Library" with Clang 11. 0.3

转载 作者:行者123 更新时间:2023-12-03 07:38:12 25 4
gpt4 key购买 nike

我已经发布了几篇与我的问题类似的帖子,但我不知道如何在我的实现中解决它。
这是关于一个自定义异常类,可以在 Linux 中使用 gcc 9 完美编译,但在 MacOs (Catalina) 中使用 Clang 11.0.3 会显着失败。
这是代码:

    #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") {}
};
在 MacOS 中编译时出现错误:
error: no matching constructor for initialization of 'mt::Exception'
ZeroDivide() : Exception("Division by near-zero value") {}
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
以及不同的注释,例如:
candidate constructor (the implicit copy constructor) not viable: no known conversion from 'const char [36]' to 'const mt::Exception' for 1st argument
以及当我尝试抛出异常时从第一个错误衍生的其他错误,例如:
Exception("Normalizing interval is ill defined")
我见过一些例子,如 this如何解决问题,但我不知道如何重新实现我的以使其在 MacOs 中工作
有人可以帮助我吗?
提前致谢。
编辑
由于项目很大,我创建了一个存储库 here用一个最小的例子来重现错误,你只需要按照这个步骤编译
mkdir build
cd build
cmake ..
make

最佳答案

您的编译器似乎在提示它不会自动转换硬编码字符串 "Division by near-zero value"您提供给 Exception 的构造函数来自 const char*const std::string& .
我目前无法访问 macos,但我的猜测是您可以通过构建 std::string 来修复它。在调用基本构造函数的地方:

ZeroDivide() : Exception(std::string("Division by near-zero value")) {}
另一个可能的解决方法是在 Exception 中定义一个构造函数。这需要 const char* :
Exception(const char* what_arg) : std::runtime_error(what_arg) {}
这可能是更好的解决方法,如 std::runtime_error 实际上有 const char* 的两个构造函数对于 const std::string& .

关于C++ 错误 : no matching constructor for initialization of "my:Library" with Clang 11. 0.3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64740833/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com