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
'サプライヤ(const Supplier&other){}':あなたのオブジェクトがベクターにコピーされるとどうなると思いますか?あなたの代入演算子も間違っています。両方を削除します。 –