jjzjj

c++ - std::runtime_error 子类的 "call to deleted constructor of"编译器错误

coder 2024-02-17 原文

我从 std::runtime_error 派生了一个异常类,以便添加对异常流的支持。我收到一个奇怪的编译器错误输出,我不确定如何解决?

clang++ -std=c++11 -stdlib=libc++ -g -Wall -I../ -I/usr/local/include Main.cpp -c
Main.cpp:43:19: error: call to deleted constructor of 'EarthException'
            throw EarthException(__FILE__, __LINE__)
                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../EarthException.hpp:9:12: note: function has been explicitly marked deleted here
    struct EarthException : public Exception<EarthException>


template <typename TDerived>
    class Exception : public std::runtime_error
    {
        public:
            Exception() : std::runtime_error("") {}

            Exception(const std::string& file, const unsigned line)
                 : std::runtime_error("")
            { 
                stream_ << (file.empty() ? "UNNAMED_FILE" : file) << "[" << line << "]: ";
            }

            virtual ~Exception() {}

            template <typename T>
            TDerived& operator<<(const T& t)
            {
                stream_ << t;
                return static_cast<TDerived&>(*this);
            }

            virtual const char* what() const throw()
            {
                return stream_.str().c_str();
            }

       private:
           std::stringstream stream_;
    };

    struct EarthException : public Exception<EarthException>
    {
        EarthException() {}

        EarthException(const std::string& file, const unsigned line)
            : Exception<EarthException>(file, line) {}

        virtual ~EarthException() {}
    };
}

更新:

我现在添加了对 std::runtime_error("") 的显式调用,因为它指出默认构造函数被标记为 =delete 但是错误仍然存​​在。

最佳答案

由于 ExceptionEarthException 中用户声明的析构函数,这些类的移动构造函数和移动赋值运算符的隐式生成被禁用。并且由于只移动数据成员 std::stringstream,隐式复制成员被删除。

然而,所有这些都是一种干扰。

你的 what 成员注定失败:

        virtual const char* what() const throw()
        {
            return stream_.str().c_str();
        }

这创建了一个右值 std::string,然后返回一个指向该临时值的指针。在客户端可以将指针读入该临时文件之前,临时文件会被破坏。

您需要做的是将 std::string 传递给 std::runtime_error 基类。然后您不需要持有 stringstream 或任何其他数据成员。唯一棘手的部分是使用正确的 string 初始化 std::runtime_error 基类:

template <typename TDerived>
    class Exception : public std::runtime_error
    {
            static
            std::string
            init(const std::string& file, const unsigned line)
            {
                std::ostringstream os;
                os << (file.empty() ? "UNNAMED_FILE" : file) << "[" << line << "]: ";
                return os.str();
            }
        public:
            Exception(const std::string& file, const unsigned line)
                : std::runtime_error(init(file, line))
            { 
            }

      private:
          <del>std::stringstream stream_;</del>

现在您将获得隐式复制成员,一切都会正常进行。

关于c++ - std::runtime_error 子类的 "call to deleted constructor of"编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12123117/

有关c++ - std::runtime_error 子类的 "call to deleted constructor of"编译器错误的更多相关文章

  1. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

    我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou

  2. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  3. ruby-on-rails - Rails - 子类化模型的设计模式是什么? - 2

    我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co

  4. ruby-on-rails - Ruby on Rails : . 常量化 : wrong constant name error? - 2

    我正在使用这个:4.times{|i|assert_not_equal("content#{i+2}".constantize,object.first_content)}我之前声明过局部变量content1content2content3content4content5我得到的错误NameError:wrongconstantnamecontent2这个错误是什么意思?我很确定我想要content2=\ 最佳答案 你必须用一个大字母来调用ruby​​常量:Content2而不是content2。Aconstantnamestart

  5. ruby - 检查 "command"的输出应该包含 NilClass 的意外崩溃 - 2

    为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar

  6. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  7. ruby-on-rails - 迷你测试错误 : "NameError: uninitialized constant" - 2

    我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test

  8. ruby-on-rails - 相关表上的范围为 "WHERE ... LIKE" - 2

    我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que

  9. 使用 ACL 调用 upload_file 时出现 Ruby S3 "Access Denied"错误 - 2

    我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file

  10. ruby-on-rails - 错误 : Error installing pg: ERROR: Failed to build gem native extension - 2

    我克隆了一个rails仓库,我现在正尝试捆绑安装背景:OSXElCapitanruby2.2.3p173(2015-08-18修订版51636)[x86_64-darwin15]rails-v在您的Gemfile中列出的或native可用的任何gem源中找不到gem'pg(>=0)ruby​​'。运行bundleinstall以安装缺少的gem。bundleinstallFetchinggemmetadatafromhttps://rubygems.org/............Fetchingversionmetadatafromhttps://rubygems.org/...Fe

随机推荐