试图在另一个 Class 中引用一个 Class 对象以避免那个 Class 的第二个实例

Attempting to Reference a Class Object Within Another Class to Avoid a 2nd Instance of that Class

1.错误解释和我的逻辑

我无法弄清楚为什么使用一个 class 中的引用对象到一个单独的 class 中并没有像我想象的那样工作。我在下面为这个例子包含了简化的代码,其中有一个 Main.cpp 和另外 2 个 classes,分别命名为 InventoryStorage 和 ShoppingExperience。

InventoryStorage class 将 .txt 文件中的名称存储到向量中,并且有一个典型的 getter,我可以在 Main.cpp 中创建一个对象来输出这些名称: invStObj.getFileInvData(i)

我的问题是我希望在其他几个 class 中也使用这个 getter,而不创建对象的新实例 InventoryStorage invStObj;制作于 Main.cpp

相反,我试图为这个对象创建一个引用,InventoryStorage& invStRefObj = invStObj;我的其他 class ShoppingExperience.cpp 也将使用此 class 中的 getFileInvData() 函数。

当我运行此代码时,它应该输出所需的输出,但实际输出在到达库存部分的项目列表时被缩短。我的第二个 class 中的参考对象无法与第一个 class 中的 getter 函数一起正常工作。
cout << invStObj.getFileInvData(i) << endl;在 Main.cpp 中正确输出“测试项目”,后跟名称
cout << invStRefObj.getFileInvData(i) << endl;来自 ShoppingExperience class 仅输出“库存中的项目列表”,然后在不输出任何名称的情况下出现错误

期望输出................................. ................... 实际输出

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 与新的空向量一起使用。使用对象的引用似乎并不能保护它免于创建该对象的新实例 class 这正是我一开始就试图阻止的。
请参考下面我的代码进行说明

2。问题: 是否可以从另一个 class 引用与我失败的尝试类似的对象?我可以使用一些替代格式或功能来实现这一目标吗?我希望 Main.cpp 和我的 ShoppingExperience.cpp 对 InventoryStorage class 函数

使用相同的对象和 class 实例

3。 InventoryStorage.h、InventoryStorage.cpp、Main.cpp、ShoppingExperience.h、ShoppingExperience.cpp 完整显示如下以供引用

InventoryStorage.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;
};

InventoryStorage.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];
}

Main.cpp

#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;
}

ShoppingExperience.h

#pragma once
#include <iostream>
using std::cout;
using std::endl;

class ShoppingExperience {
public:
    ShoppingExperience();

private:
};

ShoppingExperience.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

InventoryStorage invStRefObj 是一个全新的 object,与 main 中声明的引用无关。您需要通过引用 ShoppingExperience:

的构造函数来传递 object

main中:

ShoppingExperience shopExpObj(invStObj);

在header:

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 中创建引用。您可以直接将 object 传递给需要引用的 function/method/constructor。

您应该避免将 using namespace 放入 header 文件中,因为这些将适用于包含 header 的所有位置。