I am having trouble to access object in c++ vector. The vector is declared to hold custom class.
Class is defined as following:
class SPMgmt {
private:
vector<Part> partList;
vector<Supplier> supplierList;
public:
SPMgmt();
SPMgmt(fstream&, fstream&);
void listPart();
void listSupplier();
void searchPartBySupplierName(string);
void searchSupplierByPartCode(string);
void addNewPart();
};
And its constructor is not working right:
SPMgmt::SPMgmt(fstream& supplierFile, fstream& partFile){
string name, code, phone, streetAddr, cityStateZip;
while(std::getline(supplierFile, name)){
std::getline(supplierFile, code);
std::getline(supplierFile, phone);
std::getline(supplierFile, streetAddr);
std::getline(supplierFile, cityStateZip);
Supplier newSupplier(name, code, phone, streetAddr, cityStateZip);
// get_name() prints out supplier name
cout<<"Name: "<<newSupplier.get_name()<<endl;
// try to put object newSupplier to the vector
supplierList.push_back(newSupplier);
// PROBLEM: get_name() here did not print name. It prints empty string.
cout<<"Name: "<<supplierList[0].get_name()<<endl;
}
My question is: why the object stored in vector did not print its name properly? I can just use the get_name() function to print its name before push_back() it to the vector.
Your help is greatly appreciated.
