私はブーストで練習しており、現在ブースト共有ポインターをテストしています。ファイルを読み取ることができる Util クラスがあります。ファイルを読み取った後、「読み取り」メソッドはファイルの内容を指す boost::shared_ptr を返します。次に、この共有ポインターを Parser クラスに渡します。このクラスは、文字列を 1 行ずつ解析します。解析が完了すると、Parser クラス コンストラクターの最後 ('}') で、ブースト ヘッダー ファイルを指す「ランタイム エラー」が発生します。より具体的には、checked_delete.hpp を「delete x」行に追加します。
template<class T> inline void checked_delete(T * x) {
// intentionally complex - simplification causes regressions
typedef char type_must_be_complete[ sizeof(T)? 1: -1 ];
(void) sizeof(type_must_be_complete);
delete x;
}
単純化されたコードは次のようになります。
class File {
string _path;
public:
File(string path)
~File()
void Open();
boost::shared_ptr<string> Read();
void Close();
};
class Parse {
public:
Parse(string path) {
File file = File(path);
file.Open();
boost::shared_ptr<string> fileContentPtr = file.Read();
StartParsing(fileContentPtr);
file.Close();
}
~Parse();
StartParsing(boost::shared_ptr<string> fileContentPtr);
};
int main() {
string path = "<some path to my file>";
Parse(path);
}
誰でも私にヒントを与えることができます、私は何が間違っていますか? 前もって感謝します!
編集: 私の Read() 関数:
boost::shared_ptr<string> File::Read() {
if(file.is_open()) {
ostringstream stream;
stream << file.rdbuf();
string content = stream.str();
boost::shared_ptr<string> contentPtr(&content);
return contentPtr;
}
else {
throw std::runtime_error("File isn't opened");
}
}
「file」変数は、Open() で使用される std::fstream ファイルです。