【发布时间】:2017-02-06 14:31:08
【问题描述】:
这篇文章是我昨天帖子的延续:How to output a vector.
所以我正在编写一个简短的程序,它将字符串列表放入一个向量中,将它们导出到一个文件中,在输出它们之前查看它们,最后如果可能的话将它们删除。除了案例 3(从列表中删除一本书)之外,我已经设法让所有功能正常工作。我在 Visual Studio 中遇到的错误如下:
1.) “没有重载函数“remove”的实例与参数列表匹配。[LN 76]
2.) “‘移除’:函数不接受 2 个参数”。 [LN 76]
正如您可能知道的那样,我正在尝试按值而不是索引来删除。我仍然在这里学习所以要温柔,但为什么我会得到这些错误?
这是我的完整代码:
#include <iostream>
#include <cctype>
#include <ctype.h>
#include <algorithm>
#include <string>
#include <math.h>
#include <windows.h>
#include <iomanip>
#include <vector>
#include <iostream>
#include <sstream>
#include <fstream>
#include <istream>
// common namespace
using namespace std;
int main()
{
int option;
bool iWannaLeave = false;
vector<string> bookCollection;
string entryVal = " ";
int anotherOption;
do
{
cout << "Welcome to MyBook! - A text recorder framework application." << endl;
cout << "-----------------------------------------------------------" << endl;
cout << "Main Menu:" << endl;
cout << "1 - Add a book to the collection." << endl;
cout << "2 - Display all books currently in the collection." << endl;
cout << "3 - Remove books from the collection." << endl;
cout << "4 - Write stored collection to file." << endl;
cout << "5 - Quit" << endl;
cout << "Make your selection: ";
cin >> option;
cin.ignore();
switch (option)
{
case 1:
{
bool wannaMoreBooks = false;
// the next loop will execute at least one time so you could enter a book
do
{
wannaMoreBooks = false;
cout << "Add a book title to the collection: ";
getline(cin, entryVal);
bookCollection.push_back(entryVal);
cout << "Would you like to enter another book?(1 - yes, 0 - no): ";
cin >> anotherOption;
cin.ignore();
if (anotherOption == 1) wannaMoreBooks = true;
} while (wannaMoreBooks == true);
}
break;
case 2:
{
for (int i = 0; i < bookCollection.size(); i++)
cout << bookCollection[i] << " | ";
cout << endl;
break;
}
case 3:
{
string vecVal;
cout << "Enter the value you would like to remove: " << endl;
cin >> vecVal;
bookCollection.erase(remove(bookCollection.begin(), vecVal), bookCollection.end());
}
// remove a book from the collection
break;
case 4:
{
ofstream fileOut("Collection.txt");
fileOut << "Your MyBook Collection: [Begin] - | ";
auto first = true;
for (string x : bookCollection)
{
if (!first) { fileOut << " | "; }
first = false;
fileOut << x;
}
fileOut << " | - [End]" << endl;
cout << "Collection.txt has been successfully written." << endl;
break;
}
case 5:
{
//Nested IF to kill program properly
int quitVar;
cout << "Are you sure you want to exit the program?: ";
cin >> quitVar;
cin.ignore();
if (quitVar == 1)
{
cout << "The program will now be terminated." << endl;
iWannaLeave = true;
}
else if (quitVar == 0) cout << "Returning to the main menu." << endl;
}
break;
}
} while (iWannaLeave == false);
return 0;
}
我知道这不是近乎完美的代码,所以除了找出我收到这些错误的原因之外,我还希望对我如何改进提出一些建设性的批评。
另外:如果我想在头文件中使用函数而不是开关,我是否只需将案例内容移动到头文件中?
提前致谢! :)
【问题讨论】:
-
您没有使用正确数量的参数。你应该仔细检查reference。
-
当您解决了代码的功能问题后,您将需要前往Code Review 了解您的风格的 cmets。请记住阅读那里的介绍性材料,以便以吸引最佳答案的方式呈现它。
标签: c++