为什么我的复制构造函数被删除了?
Why is my copy constructor deleted?
给定以下代码:
#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
}
我收到以下错误:
use of deleted function 'std::__cxx11::basic_ostringstream<_CharT, _Traits, _Alloc>::basic_ostringstream(const std::__cxx11::basic_ostringstream<_CharT, _Traits, _Alloc>&) [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator]'
并且:
use of deleted function 'Format::Format(const Format&)'
并且:
invalid initialization of non-const reference of type 'TempFormat&' from an rvalue of type 'TempFormat'
谁能解释一下为什么会出现这些错误以及如何解决这些错误?
注意:我想通过此代码实现 printf
的版本(与 %
的连接)。
修复:
#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)
给定以下代码:
#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
}
我收到以下错误:
use of deleted function 'std::__cxx11::basic_ostringstream<_CharT, _Traits, _Alloc>::basic_ostringstream(const std::__cxx11::basic_ostringstream<_CharT, _Traits, _Alloc>&) [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator]'
并且:
use of deleted function 'Format::Format(const Format&)'
并且:
invalid initialization of non-const reference of type 'TempFormat&' from an rvalue of type 'TempFormat'
谁能解释一下为什么会出现这些错误以及如何解决这些错误?
注意:我想通过此代码实现 printf
的版本(与 %
的连接)。
修复:
#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)