次のクラスの例を、より大きなプロジェクトから簡略化しました。これは、ロガーのスコープを使用してデストラクタ内のログエントリを終了させるロギングフレームワークに基づいています。 std::ostringstream
オブジェクトとは何かを持っていると思われる、:コンストラクタが暗黙的に削除された機能(真実ではない編集)であるため、ストリームストリームメンバーを含むオブジェクトを構築できないのはなぜですか?
以下のコードはコンパイルされません。私は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;
}
「作業がうまくいく」というラベルが付いています。それは新しいstd::ostringstream
を含む匿名のContainer
オブジェクトをインスタンス化するようです。これは直接 "world"を出力するためにアクセスできます。 Container
自体がメッセージの「Hello」部分を作成し、そのデストラクタがバッファをフラッシュします。
Container
オブジェクトの名前が付けられて保存されている2番目の部分が正しく動作しないのはなぜですか?ここに私が得るエラーの例があります:
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>
...など。
をええと、これは[この質問](http://stackoverflow.com/q/3442520/2069064)のデュープではありません。 – Barry