1. 錯誤解釋和我的邏輯
我無法弄清楚為什么使用一個類中的參考物件到一個單獨的類中沒有按照我想象的方式作業。我在下面的示例中包含了簡化的代碼,其中包含 Main.cpp 和其他 2 個名為 InventoryStorage 和 ShoppingExperience 的類。
InventoryStorage 類將 .txt 檔案中的名稱存盤到向量中,并且有一個典型的 getter,我可以在 Main.cpp 中創建一個物件來輸出這些名稱:invStObj.getFileInvData(i)
我的問題是我希望在其他幾個類中也使用這個getter,而不創建物件的新實體,InventoryStorage invStObj;在 Main.cpp 中制作的
相反,我試圖為這個物件做一個參考,InventoryStorage& invStRefObj = invStObj; 我的其他類 ShoppingExperience.cpp 也將使用該類來利用該類中的 getFileInvData() 函式。
當我運行此代碼時,它應該輸出所需的輸出,但實際輸出在到達 Inventory 部分中的 Item List 時會縮短。我的第二類中的參考物件不能與第一類的 getter 函式一起正常作業。
cout << invStObj.getFileInvData(i) << endl; 在 Main.cpp 中正確輸出“測驗專案”,后跟名稱
cout << invStRefObj.getFileInvData(i) << endl; 從 ShoppingExperience 類只輸出“庫存中的專案串列”,然后在不輸出任何名稱的情況下出錯
期望的輸出.................................................... .... 實際輸出
test items test items
book book
movie movie
food food
game game
Item List in Inventory Item List in Inventory
book
movie
food
game
我收到一個錯誤“向量下標超出范圍”,因為我的 ShoppingExperience.cpp 正在嘗試將 getter 與新的空向量一起使用。使用物件的參考似乎并不能保護它不創建該類的新實體,這是我一開始試圖阻止的。
請參閱下面的代碼以進行說明
2. 問題: 是否可以參考另一個類中的物件,類似于我失敗的嘗試?是否有一些替代格式或功能可以用來實作這一目標?我希望 Main.cpp 和我的 ShoppingExperience.cpp 為 InventoryStorage 類函式使用相同的物件和類實體
3. InventoryStorage.h、InventoryStorage.cpp、Main.cpp、ShoppingExperience.h、ShoppingExperience.cpp 全文如下所示,以供參考
庫存存盤.h
#pragma once
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using std::string;
using std::vector;
class InventoryStorage
{
public:
InventoryStorage();
void storeFileInvData(string); //opens and stores file names to vector, invDataCl
string getFileInvData(int); //returns names from vector, invDataCl
private:
string fileNameCl;
string lineCl;
vector<string> invDataCl;
};
庫存存盤.cpp
#include "InventoryStorage.h"
InventoryStorage::InventoryStorage() {
}
void InventoryStorage::storeFileInvData(string fileNameTmp) {
fileNameCl = fileNameTmp;
std::ifstream openInvFileCl;
openInvFileCl.open(fileNameCl);
while (getline(openInvFileCl, lineCl, ',')) {
lineCl.erase(remove(lineCl.begin(), lineCl.end(), ' '));
invDataCl.push_back(lineCl);
}
}
string InventoryStorage::getFileInvData(int invDataTmp) {
return invDataCl[invDataTmp];
}
主檔案
#include <iostream>
#include "InventoryStorage.h"
#include "ShoppingExperience.h"
using namespace std;
int main() {
InventoryStorage invStObj; //obj to create instance of InventoryStorage class
InventoryStorage& invStRefObj = invStObj; //reference to obj that I can use in other classes, so that I can use same instance
invStObj.storeFileInvData("inventoryData.txt");
cout << "test items" << endl;
for (int i = 0; i < 4; i ) {
cout << invStObj.getFileInvData(i) << endl; //object and getter work fine in main, output names
}
cout << "\n";
ShoppingExperience shopExpObj; //program errors, ShoppingExperience class will not out any names
//ShoppingExperience class uses the reference object unsuccessfully
return 0;
}
購物體驗.h
#pragma once
#include <iostream>
using std::cout;
using std::endl;
class ShoppingExperience {
public:
ShoppingExperience();
private:
};
購物體驗.cpp
#include "ShoppingExperience.h"
#include "InventoryStorage.h"
ShoppingExperience::ShoppingExperience() {
InventoryStorage invStRefObj; //trying to reference first instance of object for InventoryStorage class inside ShoppingExperience class
cout << "Item List in Inventory" << endl; //only outputs "Item List in Inventory"
for (int i = 0; i < 4; i ) {
cout << invStRefObj.getFileInvData(i) << endl;
}
//no names are outputted, then errors and program stops
} //vector subscript out of range (the vector is empty)
//doesn't appear to be using object of same instance like I thought the referenced object would ensure
uj5u.com熱心網友回復:
InventoryStorage invStRefObj
是一個全新的物件,與 . 中宣告的參考無關main
。您需要通過參考將物件傳遞給以下建構式ShoppingExperience
:
在main
:
ShoppingExperience shopExpObj(invStObj);
在標題中:
class ShoppingExperience {
public:
ShoppingExperience(InventoryStorage& invStRefObj);
private:
};
在 cpp 中:
ShoppingExperience::ShoppingExperience(InventoryStorage& invStRefObj) {
cout << "Item List in Inventory" << endl; //only outputs "Item List in Inventory"
for (int i = 0; i < 4; i ) {
cout << invStRefObj.getFileInvData(i) << endl;
}
}
請注意,沒有必要在main
. 您可以將物件直接傳遞給需要參考的函式/方法/建構式。
您應該避免放入using namespace
頭檔案,因為這些將適用于包含頭檔案的所有位置。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/479279.html