我正在嘗試從文件中獲取數據並將其存儲在我創建的對象中。我一直遇到分段錯誤,但我不確定是什麼造成的。下面是代碼:爲什麼在通過引用傳遞對象時會一直出現分段錯誤?
Object<string> populateObj(Object<string>&,string);
string clean(string);
int main(){
string dictionaryFile;
ifstream inFile;
ofstream outFile;
Object<string> obj1;
cout << "Enter the name of the Dictionary file" << endl;
cin >> dictionaryFile;
obj1 = populateObj(obj1,dictionaryFile);
}
Object<string> populateObj(Object<string> &obj,string fileName){
ifstream file;
string words = "";
file.open(fileName);
if(file.is_open()){
while(!file.eof()){
file >> words;
obj.add(words);
}
}
else
cout << "could not open file..." << endl;
return obj;
}
你在初始化bag1? –
然後不通過引用:p雖然從它的外觀來看,這是一個XY問題(meta.stackoverflow.com/questions/66377/what-is-the-xy-problem)。這個代碼甚至不應該編譯。 – thang
隨機猜測,'對象'不能被正確複製 – paulm