我的主要问题是在HeaderArray.h文件中。我调用Library::getBook()成功进入该功能。它会调用Librarian::borrowBook(int bookNum, Book book[][3], int copyNum);,它成功进入该函数。 Librarian::borrowBook中的所有内容也可以正常工作。两个showName()函数均显示正确的名称。我的问题是从worker2.showBooks();调用main()时。在内部,numBooks仍为0,并且不存在workerBooks。我究竟做错了什么?在此之前,我曾尝试使用向量,但是也有与此类似的问题。谢谢!

main.cpp

#include <iostream>
//#include "Header.h" //Tried using vectors here
#include "HeaderArray.h"
using namespace std;

int main(){
Library lib; //Create library
Librarian headLib;
Librarian worker1;
Librarian worker2;
int choice = 0;
//some code
//some code
//some code
cout << "What would you like to do?\n1) Borrow copy of a book\n2)Return a copy of a book\n";
        cin >> choice;
        if (choice == 1){//Display books can borrow
            cout << "Choose a book to borrow or press 0 to return:\n";
            //worker2.showBooks();
            lib.showBooks(); //WORKS
            cin >> choice;
            if (choice == 0){
                continue;
            }
            else{
                lib.getBook(choice, worker2);
                cout << "MAIN: After rent book\n";
                worker2.showBooks();
            }
        }
system("pause");
return 0;
}


HeaderArray.h

/******************** Book *******************/
#include <iostream>
#include <string>
#include <vector>
using namespace std;

class Book{
private:
    string name;
    //int numCopies;
public:
   Book();
   void setName(string); //Mutator Function
   string getName();
   void showName();
};

Book::Book(){
    /*static int a = 1;
    a++;*/
}

void Book::setName(string n){
    name = "Book " + n;
}

string Book::getName(){
    return name;
}

void Book::showName(){
     cout << name << endl;
}


/******************** Librarian **************/
class Librarian{
    private:
        Book workerBooks[10];
        int numBooks;
    public:
        Librarian();
        void showBooks();
        void borrowBook(int, Book[][3], int);
};

Librarian::Librarian(){
    numBooks = 0;
}

void Librarian::showBooks(){
    cout << "In librarian showBooks\n";
    cout << "numBooks: " << numBooks << endl;
    //cout << "workerBooks empty? " << workerBooks.empty() << endl; //True if empty
    string name;
    for (unsigned int i = 0; i <= numBooks; i++){
        name = workerBooks[i].getName();
        //workerBooks[0].showName();
        cout << "Book name: " << name << "  Total Books: " <<  sizeof(workerBooks) / sizeof(workerBooks[0]) << endl;
   }
}

void Librarian::borrowBook(int bookNum, Book book[][3], int copyNum){
    cout << "In Librarian borrowBook\n";
    book[bookNum][copyNum].showName(); //Shows correct name
    //Book *temp = &book[bookNum][copyNum];
    //workerBooks[numBooks] = *temp;
    workerBooks[numBooks] = book[bookNum][copyNum];
    workerBooks[numBooks].showName(); //Shows correct name
    numBooks++;
    cout << "After Librarian borrowBook\n";
}

/***************** Library Class ****************/

class Library{

private: //cannot be accessed by programming statements outside of the class
    Book books[10][3];
    //Book **books;//1st * gives access to 1st dimension, 2nd * to 2nd dimension of array
protected:  //Can be accessed by inherited classes
public: //CAN be accessed by code outside the class
    Library(); //Constructor
    void getBook(int, Librarian);
    void showBooks();
};

Library::Library(){ //Constructor
    for (int i = 0; i < 10; i++){ //Library has 10 books
        for (int j = 0; j < 3; j++){ //Each book has 3 copies
            books[i][j].setName(to_string(i));
        }
    }
}

void Library::showBooks(){

    string name;
    //sizeof returns size of array in bytes.  To get length of array, divide that size by size of elements inside array
    for (unsigned int i = 0; i < sizeof(books) / sizeof(books[0]); i++){ //Library has 10 books
        for (unsigned int j = 0; j < sizeof(books[0]) / sizeof(books[0][0]); j++){ //Each book has 3 copies
            name = books[i][j].getName();
        }
        cout << i + 1 << ": " << name << "  Available: " << sizeof(books[0]) / sizeof(books[0][0]) << endl;
    }
}

void Library::getBook(int choice, Librarian worker){

    int numCopies = sizeof(books[choice]) / sizeof(books[0][0]);
    if (numCopies == 0){ //If no more copies of the book left
        cout << "We are out of those books.\n";
    }
    else{
        //can't pass entire array as an argument to function. Can pass a pointer to an array by specifying the array's name without an index.
        worker.borrowBook(choice, books, numCopies - 1);

        cout << "After Library getBook \n";
    }
}

最佳答案

这里的问题是您将Librarian对象按值传递给Library::getBook函数。传递值表示对象已复制,并且在函数内部进行的所有更改将仅在副本上进行。如您所知,修改副本不会修改原始副本。

您需要通过引用传递Librarian对象:

void getBook(int, Librarian&);

08-07 10:03