C ++类变量不会保留来自另一个类

时间:2015-10-23 05:24:38

标签: c++ arrays class object 2d

我的主要问题是在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";
    }
}

1 个答案:

答案 0 :(得分:2)

此处的问题是您将Librarian对象按值传递给Library::getBook函数。按值传递意味着对象被复制,并且在函数内所做的所有更改都将仅在副本上进行。如您所知,修改副本不会修改原件。

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

void getBook(int, Librarian&);