类变量的更新值-C++
我才刚刚开始学习如何编码,并且遇到了一个我似乎无法解决的问题。 更具体地说,问题发生在“借用”功能中。
在以下程序中,即使我使用了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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的实际问题是,您正在使用书籍对象的副本进行操作,而不是书籍成员的固定器和获取者。
您需要
,但正如其他指出的那样,您需要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.
You need
But as other have pointed out, you need 2 classes, Library and Book.
我同意托马斯·马修斯(Thomas Matthews)的评论。您当前的
book
结构没有意义,因为大多数方法用于使用book> book
s的集合而不是特定的book> book
您要将书籍集作为
library
,那么下面的代码对于在book
和library
重写
借款
根据库
方法意味着在该library
's mybooks vector上操作。当您找到特定的book
您想要在矢量内部的时,您可以调用其getters和setter,可以很好地解决您的问题,在当前代码中,这是在您的
book s并给您每个副本:
您希望它得到参考而不是副本:
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 ofBook
s rather than a particularBook
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 amongBook
andLibrary
Rewriting
borrows
in terms of aLibrary
method would mean operating on thatLibrary
'smyBooks
vector. When you find the particularBook
you want inside the vector then you can call its getters and setters which should work fineAddressing your question, in your current code this is iterating over your
Book
s and giving you a copy of each one:You want this to get a reference instead of a copy: