C ++继承向量问题(无限循环+在其他类中使用向量的问题)

我正在做一个表面让用户输入信息并打印出来。

这就是它的样子。

main <- menu <- Reservation
             <- BookingManager <- BookingRecord

And I create a vector vector<string> CompanyName in Reservation, This is outputdataInfo() that add CompanyName,

void Reservation::outputdataInfo()
{
    string CompName;
    cout << "Company Name <-" << endl;
    cin >> CompName;
    Reservation::setCompanyName(string (CompName) );
    cout << CompanyName.at(0) << endl;
    // Use for test and it works
    cout << CompanyName.size() << endl;
    // Use for test and it works
    cout << "End of Reservation, thank you." << endl;
}

公司名称的设置者:(工作)

void Reservation::setCompanyName(const string& cn)
{this->CompanyName.push_back(cn);}

But now BookingRecord::outputdataInfo() wants to print Booking Record.

void BookingRecord::outputdataInfo()
{
    cout << "      ----- Booking Record -----" << endl;
    Reservation::printBookingRecord();
}

我这样写(不确定这是正确的还是错误的):

void Reservation::printBookingRecord() {
    for (int i = 0; i < CompanyName.size(); i++) {
        cout << " ---- Company ---- " << endl;
        cout << "Name: " << CompanyName.at(i) << endl;
    }
}

But CompanyName suddenly looks like it forget anything, or like reset the size. The result is BookingRecord::outputdataInfo() is printing infinitly non-stop, but nothing happen to the Reservation::printBookingRecord(). This is weird beacuse there suppose no for-loop in BookingRecord::outputdataInfo(). And I wanna know how to print data with (Reservation::printBookingRecord() is called by BookingRecord::outputdataInfo(), but the vector is at "Reservation") (or vector can be use in other classes) Big thanks :)

评论