简体   繁体   English

为什么我的复制构造函数被删除了?

[英]Why is my copy constructor deleted?

Given the following code: 给出以下代码:

#include <iostream>
#include <vector>
#include <cstring>
#include <cassert>
#include <sstream>

using std::string;
using std::ostream;
using std::cout;
using std::endl;

typedef std::basic_ostringstream<char> ostringstream;

class Format { //error1
    const char* str;
    ostream& os;
    int index;
    ostringstream savePrint;
public:
    Format(const char* str, ostream& os) :
            str(str), os(os), index(0) {
    }

    ~Format() {
        os << savePrint.str();
    }

    template<class T>
    Format& operator<<(const T& toPrint) {
        while (index < strlen(str) && str[index] != '%') {
            savePrint << str[index++];
        }
        if (index == strlen(str)) {
            throw std::exception();
        }
        assert(str[index] == '%');
        savePrint << toPrint;
        index++;
        return *this;
    }

};

class TempFormat {
    const char* str;
public:
    TempFormat(const char* str) :
            str(str) {
    }
    friend Format operator<<(ostream& os, TempFormat& f) {
        return Format(f.str, os); //error2
    }
};

TempFormat format(const char* str) {
    return TempFormat(str);
}

int main() {
    int year = 2018;
    string hello = "Hello";
    cout << format("% world! The year is %\n") << hello << year; // error3
}

I get the following errors: 我收到以下错误:

use of deleted function 'std::__cxx11::basic_ostringstream<_CharT, _Traits, _Alloc>::basic_ostringstream(const std::__cxx11::basic_ostringstream<_CharT, _Traits, _Alloc>&) [with _CharT = char; 使用已删除的函数'std :: __ cxx11 :: basic_ostringstream <_CharT,_Traits,_Alloc> :: basic_ostringstream(const std :: __ cxx11 :: basic_ostringstream <_CharT,_Traits,_Alloc>&)[with _CharT = char; _Traits = std::char_traits; _Traits = std :: char_traits; _Alloc = std::allocator]' _Alloc = std :: allocator]'

And: 和:

use of deleted function 'Format::Format(const Format&)' 使用已删除的函数'Format :: Format(const Format&)'

And: 和:

invalid initialization of non-const reference of type 'TempFormat&' from an rvalue of type 'TempFormat' 从'TempFormat'类型的右值初始化'TempFormat&'类型的非const引用

Can someone explain me why I get these errors and how can I fix them? 有人可以解释我为什么会出现这些错误,我该如何解决?

Note: I want by this code to implement version of printf (with the connection of the % ). 注意:我希望通过此代码实现printf版本(与%的连接)。

Fixes: 修正:

#include <sstream> // defines std::basic_ostringstream

// ...

    // basic_ostringstream is movable but no copyable.
    // Make Format moveable.
    Format(Format&&) = default;

// ...

    // Cannot bind a reference to a temporary.
    // R-value reference is required here.
    friend Format operator<<(ostream& os, TempFormat&& f) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么不需要将带有已删除复制构造函数的类型的移动构造函数标记为已删除? - Why is there no need to mark the move constructor of a type with a deleted copy constructor as deleted? 为什么具有已删除的复制构造函数的结构不是POD类型? - Why is a struct with a deleted copy constructor not a POD type? 为什么我的派生类构造函数被删除? - Why is my derived class constructor deleted? 删除的复制构造函数导致删除的默认构造函数 - Deleted copy constructor results in deleted default constructor 为什么(删除)复制构造函数优先于隐式转换? - Why is (deleted) copy constructor preferred over implicit conversion? 删除副本构造函数后,为什么默认初始化失败? - Why would the default Initialization fail when copy constructor is deleted? 为什么当用户提供移动构造函数或移动赋值时,复制构造函数和复制赋值会被删除? - Why when user provides move constructor or move assignment, copy constructor and copy assignment will be deleted? 为什么在C ++中使用私有副本构造函数与已删除副本构造函数 - Why use private copy constructor vs. deleted copy constructor in C++ 删除vs空副本构造函数 - Deleted vs empty copy constructor 在单例中使用已删除的复制构造函数 - Use of deleted copy constructor in the singleton
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM