C 向量对象访问
C++ vector object access
我在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
类中的复制构建器和分配运算符都没有做任何有用的事情。
编译器会自动生成的复制构建器和分配运算符,应该为您的班级工作正常,因此您应该删除复制构建器和分配运算符,并遵循零的规则。
相关文章:
- 使用迭代器时如何访问对象在向量中的位置?
- 无法通过指针访问对象的成员
- 如何从传递到对象的对象内部访问对象的类属性?
- 有没有办法在C++中循环访问对象的不同数据成员
- 如何访问对象动态数组中的私有成员变量?
- c++ 循环访问对象列表并删除对象
- 循环访问对象列表 c++
- 访问对象的取消引用值的语法
- 如何访问对象的成员变量的取消引用值
- C++:访问对象内部子对象的方法
- 如果访问对象,则指向的对象内部的指针会更改内存位置
- 是否可以从 C++ 中的构造函数访问对象名称?
- 仅允许访问对象的成员,而不允许访问对象本身
- 从地图访问对象会导致新对象C 的实例化
- 如何在数组中访问对象内的字符串
- 在C 中运行时从指针访问对象实例
- 有关使用矢量迭代器访问对象方法的问题
- 当对象的指针存储在矢量中时,如何访问对象中的方法?
- 显式调用"运算符 new"后无法访问对象的函数
- 尝试访问对象数组中的对象,但只保存了一个对象