C 向量对象访问

C++ vector object access

本文关键字:访问 对象 向量      更新时间:2023-10-16

我在C 向量中访问对象很难。宣布向量持有自定义类。

类定义如下:

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();
};

这是班级供应商:

class Supplier{
public:
    // Constructor of an empty Supplier class object
    Supplier (){}
    // Constructor of a non-empty Supplier object
    Supplier (const string& new_name, const string& new_code,
             const string& new_phone, const string& new_strt_addr,
             const string& new_city_state_zip){
        name = new_name;
        code = new_code;
        phone_number = new_phone;
        street_address = new_strt_addr;
        city_state_zip = new_city_state_zip;
    }
    // Copy constructor
    Supplier (const Supplier& other){}
    // Assignment operator
    Supplier& operator= (const Supplier& rightSide){return *this;}
    // Destructor -releases any memory allocated to a Supplier object
    ~Supplier (void){}
    // Used to display a Supplier object to standard output
    void display (ostream& output) const;   // const was added
    // Accessor functions:
    string get_name ( ) const{return this->name;}
    string get_code ( ) const{return this->code;}
    string get_phone ( ) const{return this->phone_number;}
    string get_street_address ( ) const{return this->street_address;}
    string get_city_state_zip ( ) const{return this->city_state_zip;}
    // Mutator functions:
    void set_name (const string& new_name){this->name = new_name;}
    void set_code (const string& new_code){this->code = new_code;}
    void set_phone (const string& new_phone){this->phone_number = new_phone;}
    void set_street_address (const string& new_street_addr){this->street_address = new_street_addr;}
    void set_city_state_zip(const string& new__city_st_zip){this->city_state_zip = new__city_st_zip;}
private:
    string name;
    string code;           // Addd where A is an upper case letter and
                          // the d's are digits
    string phone_number;
    string street_address;
    string city_state_zip;
};

及其构造函数无法正常工作:

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

我的问题是:为什么存储在向量中的对象未正确打印其名称?我可以在push_back()之前使用get_name()函数打印其名称。

Supplier类中的复制构建器和分配运算符都没有做任何有用的事情。

编译器会自动生成的复制构建器和分配运算符,应该为您的班级工作正常,因此您应该删除复制构建器和分配运算符,并遵循零的规则。