我正在爲我的架構類寫一個分支預測模擬器,並且在多個類之間獲得輸出工作時遇到了一些麻煩。我試圖在main.cpp中打開一個mainstream,並將其傳遞給每個類的構造函數以供對象使用。我收到了一大堆的錯誤在我的頭文件,如:錯誤當試圖傳遞一個對象到一個對象(C++)
In file included from main.cpp:4:0:
predictors.h: In constructor â?~ATPred::ATPred(std::ofstream&)â?T:
predictors.h:14:18: error: use of deleted function â?~std::basic_ofstream<char>& std::basic_ofstream<char>::operator=(
const std::basic_ofstream<char>&)â?T
因此,錯誤似乎是在我的頭文件:
#ifndef PREDICTORS_H
#define PREDICTORS_H
#include <string>
// Always Taken
class ATPred{
private:
std::ofstream outputFile;
public:
// Constructor
ATPred(std::ofstream& file)
{ outputFile = file; }
// Deconstructor
~ATPred()
{}
// Member Functions
void parseResults(std::string filename);
};
// Always Non-Taken
class ANTPred{
private:
std::ofstream outputFile;
public:
// Constructor
ANTPred(std::ofstream& file)
{ outputFile = file; }
// Deconstructor
~ANTPred()
{}
// Member Functions
void parseResults(std::string filename);
};
#endif
我不知道在哪裏,我在這裏出錯,所以任何幫助將不勝感激。
我不會在主函數中關閉ofstream,直到完成所有通過對象的操作,仍然是不安全的? – Riptyde4
這只是有風險的,因爲編譯器沒有編譯時可強制執行的合約。在我看來,如果這是應用程序的設計並且每個人都知道正在處理代碼的情況,那就沒問題。 – Nathan
gotcha,謝謝!我是目前唯一處理代碼的人,所以我認爲這種模式沒有問題。 – Riptyde4