我在代码中遇到了一些错误,因为我是C ++的新手,我正在努力弄清楚我做错了什么以及解决方案是什么。我用Google搜索了一下,但我发现没有什么是有道理的,所以如果有人能给我一些帮助,那就太棒了,这样我就可以从中学习。
错误全都在第23行,即搜索(令牌);
Error C2672 'search': no matching overloaded function found TakeTwo d:\desktop\twittersearch\taketwo\taketwo\taketwo.cpp 23
Error C2780 '_FwdIt1 std::search(_FwdIt1,_FwdIt1,_FwdIt2,_FwdIt2)': expects 4 arguments - 1 provided TakeTwo d:\desktop\twittersearch\taketwo\taketwo\taketwo.cpp 23
Error C2780 '_FwdIt1 std::search(_FwdIt1,_FwdIt1,_FwdIt2,_FwdIt2,_Pr)': expects 5 arguments - 1 provided TakeTwo d:\desktop\twittersearch\taketwo\taketwo\taketwo.cpp 23
代码:
#include "stdafx.h"
#include <cstring>
#include <fstream>
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
string token;
int main()
{
int menu_choice;
cout << "Main Menu:\n";
cout << "1. Search for \"winner\" \n";
cout << "Please choose an option: ";
cin >> menu_choice;
if (menu_choice == '1') {
token == "winner";
search(token);
} else {
cout << "\nPlease enter a valid option\n";
system("Pause");
system("cls");
main();
}
return 0;
}
void search(string &token)
{
ifstream fin;
fin.open("sampleTweets.csv");
if (fin.is_open())
{
cout << "File opened successfully" << "\n";
}
else {
cout << "Error opening file" << "\n";
}
string line;
while (getline(fin, line)) {
if (line.find(token) != string::npos) {
int n = line.find(",");
char c;
line[n] = ' '; //Changes the comma spacing
line.erase(remove_if(line.begin(), line.end(), [](char chr) { return chr == '\"' || chr == '\'' || chr == ','; }), //Removes the characters " ' and , from the strings
line.end());
line.erase(n + 1, 1); //Removes the 'b' from the beginning of each tweet
cout << line << endl;
}
}
fin.close();
char anykey;
cout << "press any key";
cin >> anykey;
return;
}
答案 0 :(得分:3)
using namespace std;
的危险。由于您在使用之前从未声明或定义search
函数,因此编译器假设您的意思是std::search
,它是<algorithm>
标头的一部分。在使用之前确保declare您的功能将在这种情况下解决错误。
#include <cstring>
#include <fstream>
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
string token;
// Forward declare search before first use
void search(string &token);
int main()
{
int menu_choice;
cout << "Main Menu:\n";
cout << "1. Search for \"winner\" \n";
cout << "Please choose an option: ";
cin >> menu_choice;
if (menu_choice == '1') {
token == "winner";
search(token);
}
else {
cout << "\nPlease enter a valid option\n";
system("Pause");
system("cls");
main();
}
return 0;
}