「暗黙的に削除されたコピー コンストラクターの呼び出し」コンパイル エラーの問題を、クラスのメンバーを宣言する際の ostringstream 型の使用に切り分けました。以下の例では、例の Reading クラスのオブジェクトの STL リストが定義されています。push_back が呼び出された時点で、コンパイラはコピー コンストラクターを検索し、コンパイルは失敗します。これは、Readings のコピー コンストラクターが暗黙的に削除されたためと思われます。
ペイロード文字列を参照する 2 行をコメント アウトすると、プログラムがコンパイルされます。
ここで説明されているように、私の問題は ostringstream が参照型である可能性があると考えています:
https://en.cppreference.com/w/cpp/language/copy_constructor 「T には右辺値参照型のデータ メンバーがあります。」コピー コンストラクターが暗黙的に削除される理由の 1 つとして挙げられます。
Q. ostringstream が参照型であり、問題を引き起こしているという私の上記の仮定が正しいかどうか、誰でも確認できますか?
私が ostringstream を使用している理由は、この不自然な例では明らかではありません。この文字列を処理する別の方法を見つける必要があるかもしれませんが、ここで機能するアプローチを誰か提案してもらえますか?
// testing a problem where ostringstream causes implicitly deleted copy constructor
//
// using ostringstream in a class definition seems to cause implicit deletion of the copy constructor
#include <iostream>
#include <sstream>
#include <list>
#include <string>
using namespace std;
class Reading {
public:
double elevation;
std::ostringstream payloadString; // using ostringstream here causes implicit deletion of the copy constructor
double speed;
// constructors and member functions
Reading(); // initialisation constructor declaration
private:
};
Reading::Reading(): // initialisation constructor definition
elevation(0.0),
payloadString("_null_null_"), // commenting out this line and the previous definition in the class makes the problem go away
speed(0.0)
{}
int main()
{
std::list<Reading> readingsList; // a list of readings
Reading fakeReading; // just initialises with dummy data
// this line is what causes the compiler to complain about implicitly deleted copy constructors
readingsList.push_back(fakeReading);
return 0;
}