【问题标题】:Error on getline function no instance matches the argumentsgetline 函数上的错误没有实例与参数匹配
【发布时间】:2021-09-04 00:58:43
【问题描述】:

为什么我的代码没有执行并显示错误?我在这条线上遇到错误 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, word, 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, word, ' , '))
        { // 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;
}

【问题讨论】:

  • 为什么不把你得到的错误也加进去?
  • 头文件丢失#include 导致错误no instance of overloaded function getline matches the argument list 我使用没有头文件的字符串流。忘记添加了!

标签: c++ visual-studio visual-c++ c++17


【解决方案1】:

问题来了:

string s(line);
while (getline(s, word, ' , '))

因为getline 没有将std::string 作为其第一个参数的重载。

但是,有一个需要 stringstream 的重载,所以你可以这样做:

stringstream ss(line);
while (getline(ss, word, ' , '))

另外,' , ' 不会按照你的想法去做。也许你的意思是','

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

【讨论】:

  • 我修复了你所说的一切。现在它显示incomplete type is not allowedno instance of overloaded function "getline" matches the argument list
  • 添加#include &lt;sstream&gt;
  • 啊啊啊所以头文件不见了!
【解决方案2】:

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

【讨论】:

  • @ThundeReX 是否详细说明了错误消息?就像它说的是预期的参数列表与您给出的参数列表一样吗?
  • 不,它只是说no instance of overloaded function "getline" matches the argument listincomplete type is not allowed
猜你喜欢
  • 2023-03-10
  • 2014-12-27
  • 1970-01-01
  • 2013-11-17
  • 1970-01-01
  • 2013-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多