类变量的更新值-C++

发布于 2025-01-21 12:52:08 字数 1449 浏览 4 评论 0原文

我才刚刚开始学习如何编码,并且遇到了一个我似乎无法解决的问题。 更具体地说,问题发生在“借用”功能中。

在以下程序中,即使我使用了getters和setter,我也无法以某种方式更新公共类变量“库存”的价值。 它似乎在Cout上正确更新,但不能“永久保存”。我的猜测是,它正在修改变量的副本,而不是变量本身。

我已将代码附加到帖子上!请让我知道是否应该上传整个文件!

提前致谢!

void Book::borrows() {
    int searchid;
    bool isfound=false;
    cout<<"Please enter the unique ID of the book:\t\t";
    cin>>searchid;
    for(auto i:myBooks){
        if (i.id==searchid){
            cout<<"This book matches your search:\t"; print(i);
            if (i.stock==0) {
                cout<<"Book is out of stock!"<<endl;
            } else {
                setStock((i.stock-1));
                cout<<"Successfully borrowed!! Now there are only "<<getStock()<<" copies left in stock!"<<endl;
            }
            isfound=true;
        }
    }
    if (isfound== false){
        cout<<"++++\t\tBook not found++++\t\t"<<endl;
    }
    system("pause");
}

int Book::getStock() const {
    return stock;
}

void Book::setStock(int newstock) {
    Book::stock = newstock;
}

编辑1:

这是我的班级结构和我的向量:

class Book{
public:
    int id;
    string title;
    int stock;

    void add();
    void displayall();
    void displayspecific();
    void print(Book);
    void borrows();
    void returns();

    int getStock() const;

    void setStock(int stock);
};

vector<Book> myBooks;

I'm just beginning to learn how to code and I've come across a problem I can't seem to solve.
More specifically the problem occurs in the "borrows" function.

In the following program I am somehow unable to update the value of the public class variable "stock" even though I used getters and setters.
It seems to be updated correctly on the cout right after but not "saved permanently". My guess is that it's modifying a copy of the variable rather than the variable itself.

I have attached my code to the post! Please let me know if I should upload the whole file!

Thanks in advance!

void Book::borrows() {
    int searchid;
    bool isfound=false;
    cout<<"Please enter the unique ID of the book:\t\t";
    cin>>searchid;
    for(auto i:myBooks){
        if (i.id==searchid){
            cout<<"This book matches your search:\t"; print(i);
            if (i.stock==0) {
                cout<<"Book is out of stock!"<<endl;
            } else {
                setStock((i.stock-1));
                cout<<"Successfully borrowed!! Now there are only "<<getStock()<<" copies left in stock!"<<endl;
            }
            isfound=true;
        }
    }
    if (isfound== false){
        cout<<"++++\t\tBook not found++++\t\t"<<endl;
    }
    system("pause");
}

int Book::getStock() const {
    return stock;
}

void Book::setStock(int newstock) {
    Book::stock = newstock;
}

Edit 1:

Here is my Class structure and my vector:

class Book{
public:
    int id;
    string title;
    int stock;

    void add();
    void displayall();
    void displayspecific();
    void print(Book);
    void borrows();
    void returns();

    int getStock() const;

    void setStock(int stock);
};

vector<Book> myBooks;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

往事随风而去 2025-01-28 12:52:09

您的实际问题是,您正在使用书籍对象的副本进行操作,而不是书籍成员的固定器和获取者。

for(auto i:myBooks){

您需要

for(auto &i:myBooks){

,但正如其他指出的那样,您需要2堂课,图书馆和书籍。

Your actual problem is that you are operating on a copy of the Book object, not the setters and getters of the members of a book.

for(auto i:myBooks){

You need

for(auto &i:myBooks){

But as other have pointed out, you need 2 classes, Library and Book.

埋情葬爱 2025-01-28 12:52:09

我同意托马斯·马修斯(Thomas Matthews)的评论。您当前的book结构没有意义,因为大多数方法用于使用book> book s的集合而不是特定的book> book

您要将书籍集作为library,那么下面的代码对于在booklibrary

#include <string>
#include <vector>

using namespace std;

class Book {
public:
    int id;
    string title;
    int stock;

    // These methods operate on this particular Book instance
    int getStock() const;
    void setStock(int);
};

int Book::getStock() const {
    return stock;
}

void Book::setStock(int newstock) {
    stock = newstock;
}

class Library {
public:
    vector<Book> myBooks;

    // These methods operate on the vector of books
    void add();
    void displayall();
    void displayspecific();
    //void print(Book); // This method probably belongs inside Book
    void borrows();
    void returns();
};

int main() {
    return 0;
}

重写借款根据方法意味着在该library's mybooks vector上操作。当您找到特定的book您想要在矢量内部的时,您可以调用其getters和setter,可以很好地

解决您的问题,在当前代码中,这是在您的book s并给您每个副本:

for(auto i:myBooks)

您希望它得到参考而不是副本:

for(auto& i:myBooks)

I agree with Thomas Matthews' comment. Your current Book structure does not make sense because most of the methods are for doing work with a collection of Books rather than a particular Book

If you were to refer to a collection of books as a Library then the code below would make more sense for delegating where work is done among Book and Library

#include <string>
#include <vector>

using namespace std;

class Book {
public:
    int id;
    string title;
    int stock;

    // These methods operate on this particular Book instance
    int getStock() const;
    void setStock(int);
};

int Book::getStock() const {
    return stock;
}

void Book::setStock(int newstock) {
    stock = newstock;
}

class Library {
public:
    vector<Book> myBooks;

    // These methods operate on the vector of books
    void add();
    void displayall();
    void displayspecific();
    //void print(Book); // This method probably belongs inside Book
    void borrows();
    void returns();
};

int main() {
    return 0;
}

Rewriting borrows in terms of a Library method would mean operating on that Library's myBooks vector. When you find the particular Book you want inside the vector then you can call its getters and setters which should work fine

Addressing your question, in your current code this is iterating over your Books and giving you a copy of each one:

for(auto i:myBooks)

You want this to get a reference instead of a copy:

for(auto& i:myBooks)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文