C++ 벡터에서 객체에 액세스하는 데 문제가 있습니다. 벡터는 사용자 정의 클래스를 보유하도록 선언됩니다.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() 함수를 사용할 수 있습니다.
나는 당신의 quesiton을 이해하게하십시오. 정의를 보여주지 않은 클래스가 올바르게 작동하지 않는 이유를 묻습니다. 누군가가 당신의 정의를 제공하지 않는 '공급자'클래스에 대해 당신에게 어떻게 잘못된 것을 말할 것이라고 기대합니까? –
죄송합니다. 공급 업체를 게시하는 것을 잊어 버렸습니다. – H72L76
'Supplier (const Supplier & other) {}': 객체가 벡터에 복사되면 어떻게 될 것으로 생각하십니까? 할당 연산자도 잘못되었습니다. 둘 다 삭제하십시오. –