微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

getline 函数上的错误没有实例与参数匹配

如何解决getline 函数上的错误没有实例与参数匹配

为什么我的代码没有执行并显示错误?我在这条线上出错 while (getline(s,word,',')) 我的代码如下:

#include <fstream> 
#include <string>
#include <string.h>
#include <algorithm> 
#include <iostream> 
#include <vector>

using namespace std;
// first we define a class that will represent all the candidates 
class Candidate
{
    string name;
    int Votes;
public:
    Candidate()
    {
        name = "";
        int Votes = 0;
    }
    Candidate(string cand_name,int Vote_count)
    {
        name = cand_name; Votes = Vote_count;
    } string getName() { return name; } int getVotes() { return Votes; } void get_details() { cout << name << "," << Votes << endl; }
    //Following member method is used to increment the Vote count
    void Vote_this_candidate() { Votes++; }
};
int main()
{
    cout << "Welcome to Student President Voting System!!!" << endl;
    Candidate allCandidates[100];
    int totalVotes = 0;
    // File pointer fstream fin; // Open an existing file
    fstream fin;
    fin.open("candidantes.txt",ios::in); // Read the Data from the file // as String Vector 
    vector <string> row;
    string line,temp; int index = 0; // Following while loop will iterate for each line in the file
    while (fin >> temp) {
        row.clear(); // read an entire row and // store it in a string variable 'line' 
        getline(fin,line); // used for breaking words 
        string s(line); // read every column data of a row and // store it in a string variable,'word'
        while (getline(s,'))
        { // adding the splitted words to row
            row.push_back(word);
        } allCandidates[index] = Candidate(row[0],stoi(row[1])); totalVotes += stoi(row[1]); index++;
    }
    string name = ""; cout << "\nPlease enter the name of the candidante you want to Vote : ";
    getline(cin,name); int cand_no = -1; string userChoice; int i = 0; //Now we find the candidante with the same inputted name
    while (i < index) {
        if (allCandidates[i].getName() == " " + name) {
            cand_no = i; cout << "Do you want to Vote this candidante [y/n] : ";
            cin >> userChoice; //After finding the candidate just ask the user to Vote the candidante
            if (userChoice == "y") { //to Vote just call the member method that increments the Vote count
                allCandidates[cand_no].Vote_this_candidate(); totalVotes++; cout << endl << "You successfully Voted to " << name << " Thanks for voting!!!" << endl;
            }
            else { cout << "You didn't Vote!!!" << endl; } break;
        }
        i++;
    } if (cand_no == -1) {
        cout << "Candidante not found!!! Do you like to add this candidate [y/n]: ";
        cin >> userChoice; if (userChoice == "y") { allCandidates[index + 1] = Candidate(name,1); totalVotes++; index++; }
    }
    //To show top five candidates we first sort the array with lambda 
    std::sort(allCandidates,allCandidates + 10,[](Candidate a,Candidate b) -> bool { return a.getVotes() > b.getVotes(); });
    //then we show only first five candidates 
    cout << endl << "These are top 5 candidantes so far : " << endl;
    for (int i = 0; i < 5; i++)
    {
        cout << i + 1 << ","; allCandidates[i].get_details();
    } cout << endl << "Total studnets Voted: " << totalVotes;
}

解决方法

问题出在这里:

az aks get-versions --location eastus --output table

因为 string s(line); while (getline(s,word,',')) 没有将 getline 作为其第一个参数的重载。

但是,有一个需要 std::string 的重载,因此您可以这样做:

stringstream

此外,stringstream ss(line); while (getline(ss,')) 不会按照您的想法行事。也许你的意思是','

最后,',' 构造函数中的 int votes = 0; 应该只是 Candidate()。实际上,您只是在声明、初始化然后丢弃一个局部变量。

,

问题是编译器告诉您您提供的参数与函数的定义不匹配。在您的情况下,我认为问题在于您在字符部分给了 3 个字符而不是 1 个字符(请记住,空格也是一个字符)。尝试将 ',' 更改为 ','

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。