C++为什么我的对象被破坏,有时在使用基于范围的for循环时会连续两次被破坏



我是一名初级程序员,希望了解为什么我的对象被删除,有时甚至被删除两次。我尽量避免在这个项目的堆上创建它们,因为这是一个更先进的主题,我稍后会尝试。

是什么导致book1、book2等对象被立即删除?它还输出同一本书对象,因为某种原因连续删除两次有时?当通过菜单在switch语句中使用"case 3:"时,也会出现同样的效果。

#include <iostream>
#include <vector>
using namespace std;
class Book
{
public:
// Constructor Prototype
Book(string author = "Auth", string title = "Title", string publisher = "Pub", float price = 1.99, int stock = 1);
// Destructor Prototype
~Book();
// Methods
string get_title() const
{
return title;
}
private:
string author;
string title;
string publisher;
float price;
int stock;
};
// Constructor definition
Book::Book(string author, string title, string publisher, float price, int stock)
: author{author}, title{title}, publisher{publisher}, price{price}, stock{stock}
{
cout << "Book: " << title << " was created." << endl;
}
// Destructor definition
Book::~Book()
{
cout << "Book: " << title << " has been destroyed." << endl;
}
// Functions
void showMenu();
void getChoice();
void userChoice(int choice);
void showBooks(vector<Book> bookList);
// Global Vector
vector<Book> bookList;
int main()
{
// Creating Book objects for testing purposes
Book book1("Marcel Proust", "In Search of Lost Time", "Pub", 14.99, 5);
Book book2("James Joyce", "Ulysses", "Pub", 25.99, 4);
Book book3("Miguel de Cervantes", "Don Quixote", "Pub", 35.99, 3);
Book book4("Gabriel Garcia Marquez", "One Hundred Years of Solitude", "Pub", 100.99, 2);
Book book5("F. Scott Fitzgerald", "The Great Gatsby", "Pub", 49.99, 1);
// Pushing book1-5 into the vector of Book objects
bookList.push_back(book1);
bookList.push_back(book2);
bookList.push_back(book3);
bookList.push_back(book4);
bookList.push_back(book5);
while(true)
{
showMenu();
getChoice();
}
return 0;
}
void showMenu()
{
cout << "tMENU" << endl;
cout << "1. Purchase Book" << endl;
cout << "2. Search for Book" << endl;
cout << "3. Show Books available" << endl;
cout << "4. Add new Book" << endl;
cout << "5. Edit details of Book" << endl;
cout << "6. Exit" << endl;
}
void getChoice()
{
int choice;
cout << "Enter your choice (1-6): ";
cin >> choice;
userChoice(choice);
}
void userChoice(int choice)
{
switch(choice)
{
case 1:
cout << "Case 1 called." << endl;
break;
case 2:
cout << "Case 2 called." << endl;
break;
case 3:
cout << "Case 3 called." << endl;
showBooks(bookList);
break;
case 4:
cout << "Case 4 called." << endl;
break;
case 5:
cout << "Case 5 called." << endl;
break;
case 6:
cout << "Case 6 called." << endl;
exit(0);
}
}
void showBooks(vector<Book> bookList)
{
for (Book bk : bookList)
{
cout << "Book: " << bk.get_title() << endl;
}
}

编辑:

为什么它有时会连续删除同一个对象2-3次?输出(在这种情况下是"寻找失去的时间"(:

Book: In Search of Lost Time was created.
Book: Ulysses was created.
Book: Don Quixote was created.
Book: One Hundred Years of Solitude was created.       
Book: The Great Gatsby was created.
Book: In Search of Lost Time has been destroyed.       
Book: In Search of Lost Time has been destroyed.       
Book: Ulysses has been destroyed.
Book: In Search of Lost Time has been destroyed. 

此处

for (Book bk : bookList)

你在复印书。改为

for (Book &bk : bookList)

现在你只有一个参考

根据评论中的各种建议,我已经编译了该程序以满足您的需求。它将在没有任何不必要的分配的情况下工作。我所做的更改是:1.将向量保留为5个元素。2.安置。3.传递向量作为对函数的引用和4。迭代时以向量元素为参考。

#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Book
{
public:
// Constructor Prototype
Book(string author = "Auth", string title = "Title", string publisher = "Pub", float price = 1.99f, int stock = 1);
// Destructor Prototype
~Book();
// Methods
string get_title() const
{
return title;
}
private:
string author;
string title;
string publisher;
float price;
int stock;
};
// Constructor definition
Book::Book(string author, string title, string publisher, float price, int stock)
: author( author ), title( title ), publisher( publisher ), price( price ), stock( stock )
{
cout << "Book: " << title << " was created." << endl;
}
// Destructor definition
Book::~Book()
{
cout << "Book: " << title << " has been destroyed." << endl;
}
// Functions
void showMenu();
void getChoice();
void userChoice(int choice);
void showBooks(vector<Book>& bookList);
// Global Vector
vector<Book> bookList;
int main()
{
bookList.reserve(5);
bookList.emplace_back("Marcel Proust", "In Search of Lost Time", "Pub", 14.99f, 5);
bookList.emplace_back("James Joyce", "Ulysses", "Pub", 25.99f, 4);
bookList.emplace_back("Miguel de Cervantes", "Don Quixote", "Pub", 35.99f, 3);
bookList.emplace_back("Gabriel Garcia Marquez", "One Hundred Years of Solitude", "Pub", 100.99f, 2);
bookList.emplace_back("F. Scott Fitzgerald", "The Great Gatsby", "Pub", 49.99f, 1);
while (true)
{
showMenu();
getChoice();
}
return 0;
}
void showMenu()
{
cout << "tMENU" << endl;
cout << "1. Purchase Book" << endl;
cout << "2. Search for Book" << endl;
cout << "3. Show Books available" << endl;
cout << "4. Add new Book" << endl;
cout << "5. Edit details of Book" << endl;
cout << "6. Exit" << endl;
}
void getChoice()
{
int choice;
cout << "Enter your choice (1-6): ";
cin >> choice;
userChoice(choice);
}
void userChoice(int choice)
{
switch (choice)
{
case 1:
cout << "Case 1 called." << endl;
break;
case 2:
cout << "Case 2 called." << endl;
break;
case 3:
cout << "Case 3 called." << endl;
showBooks(bookList);
break;
case 4:
cout << "Case 4 called." << endl;
break;
case 5:
cout << "Case 5 called." << endl;
break;
case 6:
cout << "Case 6 called." << endl;
exit(0);
}
}
void showBooks(vector<Book>& bookList)
{
for (const auto& book : bookList){
cout << "Book: " << book.get_title() << endl;
}
}

最新更新