我有以下类示例,从一个更大的项目中简化而来。它基于一个日志框架,该框架使用记录器的范围来终止析构函数中的日志条目。
下面的代码不会编译,因为构造函数是一个隐式删除的函数(edit: not true),这似乎与std::ostringstream
对象有关。我对此感到困惑,因为我认为我应该能够直接构造 a std::ostringstream
,这意味着我应该能够直接构造一个Container
对象。
#include <iostream>
#include <sstream>
class Container {
public:
std::ostringstream bufferStream;
public:
Container(); // constructor
~Container();
};
Container::Container() {
bufferStream << "Hello ";
}
Container::~Container() {
std::cout << bufferStream.str() << " [end]" << std::endl;
}
// === Main method ===
int main() {
Container().bufferStream << "world"; // works fine
{ // causes tons of compiler errors
Container cont = Container();
cont.bufferStream << "world!";
}
return 0;
}
请注意,标有“工作正常”的行就是这样做的。好像是实例化了一个匿名Container
对象,里面包含一个 new std::ostringstream
,可以直接访问输出“world”。自身创建消息的Container
“Hello”部分,其析构函数刷新缓冲区。
为什么Container
命名和保存对象的第二部分不能正确运行?这是我得到的错误示例:
error.cpp: In function ‘int main()’:
error.cpp:28:36: error: use of deleted function ‘Container::Container(const Container&)’
Container cont = Container();
^
error.cpp:4:7: note: ‘Container::Container(const Container&)’ is implicitly deleted because the default definition would be ill-formed:
class Container {
^
error.cpp:4:7: error: use of deleted function ‘std::basic_ostringstream<char>::basic_ostringstream(const std::basic_ostringstream<char>&)’
In file included from error.cpp:2:0:
/usr/include/c++/4.8/sstream:387:11: note: ‘std::basic_ostringstream<char>::basic_ostringstream(const std::basic_ostringstream<char>&)’ is implicitly deleted because the default definition would be ill-formed:
class basic_ostringstream : public basic_ostream<_CharT, _Traits>
... 等等。