1。错误和我的逻辑解释
我很难理解为什么将一个引用的对象从一个类中使用到一个单独的类中并不能像我想象的那样工作。我在下面为这个例子包含了一个Main.cpp和另外两个类,名为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<lt;invStObj.getFileInvData(i)<lt;endl;在Main.cpp中正确地输出";测试项目";然后是名称
cout<lt;invStRefObj.getFileInvData(i)<lt;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、Shopping Experience.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;
}
购物体验.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
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
放在头文件中,因为这些文件将应用于包含头的所有位置。