gpt4 book ai didi

c++ - 需要有关 getline 函数错误 : no matching function for call to 'getline(std::istream&, char&)' | 的编译错误的帮助

转载 作者:行者123 更新时间:2023-11-28 08:16:32 27 4
gpt4 key购买 nike

我有一个带有 getline 函数的源代码文件,当我编译它时收到错误(下面的代码和错误)。我的问题是我从一个已经编译和工作的程序(也包括在下面)中复制并粘贴了整个函数。我在程序的其他 2 个源代码文件中也有 getline 函数,它们都可以正常编译。我对编程还很陌生,刚开始进行 C++ 编程(在 Java 方面要好得多),所以尽量让答案保持简单。我在这里(和谷歌)查看了一些已经发布的问题和答案,但我找到的所有问题和答案都说函数的参数不正确。但我知道它们在这里是正确的,因为它在其他程序中工作得很好。此外,正如您在工作文件中看到的那样,唯一的 #included 是 iostream 并且它可以工作,这是在 Code::Blocks 中使用 g++ 编译器。我确保也包括所有需要的变量/常量。

这是我从中得到错误的函数和文件的代码。我所指的部分用 3 * 标记(抱歉,我尽力了)。该错误也在帖子底部重印。

#include <iostream>
#include <istream>
#include "candidates.h"

using namespace std;

int nCandidatesInPrimary;

int delegatesForThisState;

const int maxCandidates = 10;

int delegatesWon[maxCandidates];

int totalVotes;

int totalDelegates = 0;

int votesForCandidate[maxCandidates];

string candidate[maxCandidates];

int nCandidates;

string candidateNames;

void readCandidates ()
{
cin >> nCandidates;
string line;
getline (cin, line);

for (int i = 0; i < nCandidates; ++i)
{
***getline (cin, candidateNames[i]);***
delegatesWon[i] = 0;
}
}

int findCandidate (std::string name)
{
int result = nCandidates;
for (int i = 0; i < nCandidates && result == nCandidates; ++i)
if (candidateNames[i] == name)
result = i;
return result;
}

void printCandidateReport (int candidateNum)
{
int requiredToWin = (2 * totalDelegates + 2) / 3; // Note: the +2 rounds up
if (delegatesWon[candidateNum] >= requiredToWin)
cout << "* ";
else
cout << " ";
cout << delegatesWon[candidateNum] << " " << candidateNames[candidateNum] << endl;
}

void assignDelegatesToCandidates ()
{
int remainingDelegates = delegatesForThisState;
for (int i = 0; i < nCandidatesInPrimary; ++i)
{
int candidateNum = findCandidate(candidate[i]);
int nDel = (delegatesForThisState * votesForCandidate[i] + (totalVotes-1)) / totalVotes;
if (nDel > remainingDelegates)
nDel = remainingDelegates;
delegatesWon[candidateNum] += nDel;
remainingDelegates -= nDel;
}
}

这是已经运行的程序的代码。

#include <iostream>

using namespace std;

// Max # of candidates permitted by this program
const int maxCandidates = 10;

// Names of the candidates participating in this state's primary
string candidate[maxCandidates];

// Names of all candidates participating in the national election
std::string candidateNames[maxCandidates];

// How many delegates are assigned to the state being processed
int delegatesForThisState;

// How many delgates have been won by each candidate
int delegatesWon[maxCandidates];

// How many candidates in the national election?
int nCandidates;

// How many candidates in the primary for the state being processed
int nCandidatesInPrimary;

// How many states participate in the election
int nStates;

// How many delegates in the election (over all states)
int totalDelegates = 0;

// How many votes were cast in the primary for this state
int totalVotes;

// How many votes wone by each candiate in this state's primary
int votesForCandidate[maxCandidates];


int findCandidate (std::string name);

/**
* For the most recently read primary, determine how many delegates have
* been won by each candidate.
*/
void assignDelegatesToCandidates ()
{
int remainingDelegates = delegatesForThisState;
for (int i = 0; i < nCandidatesInPrimary; ++i)
{
int candidateNum = findCandidate(candidate[i]);
int nDel = (delegatesForThisState * votesForCandidate[i] + (totalVotes-1)) / totalVotes;
if (nDel > remainingDelegates)
nDel = remainingDelegates;
delegatesWon[candidateNum] += nDel;
remainingDelegates -= nDel;
}
}



/**
* Find the candidate with the indicated name. Returns the array index
* for the candidate if found, nCandidates if it cannot be found.
*/
int findCandidate (std::string name)
{
int result = nCandidates;
for (int i = 0; i < nCandidates && result == nCandidates; ++i)
if (candidateNames[i] == name)
result = i;
return result;
}


/**
* Print the report line for the indicated candidate
*/
void printCandidateReport (int candidateNum)
{
int requiredToWin = (2 * totalDelegates + 2) / 3; // Note: the +2 rounds up
if (delegatesWon[candidateNum] >= requiredToWin)
cout << "* ";
else
cout << " ";
cout << delegatesWon[candidateNum] << " " << candidateNames[candidateNum] << endl;
}


/**
* read the list of candidate names, initializing their delegate counts to 0.
*/
void readCandidates ()
{
cin >> nCandidates;
string line;
getline (cin, line);

for (int i = 0; i < nCandidates; ++i)
{
***getline (cin, candidateNames[i]);***//already working
delegatesWon[i] = 0;
}
}


/**
* read the info on one state's primaries
*/
void readState ()
{
totalVotes = 0;
cin >> nCandidatesInPrimary >> delegatesForThisState;
totalDelegates += delegatesForThisState; // "x += y" is a shorthand for "x = x + y"
string word, line;
getline (cin, line);
for (int i = 0; i < nCandidatesInPrimary; ++i)
{
cin >> votesForCandidate[i];
totalVotes = totalVotes + votesForCandidate[i];

cin >> word;
getline (cin, line);
candidate[i] = word + line;
}
}



/**
* Generate the report on the national primary election.
*/
int main(int argc, char** argv)
{
readCandidates();
int nStates;
cin >> nStates;
for (int i = 0; i < nStates; ++i)
{
readState();
assignDelegatesToCandidates();
}
for (int i = 0; i < nCandidates; ++i)
{
printCandidateReport(i);
}
return 0;
}

错误:没有用于调用“getline(std::istream&, char&)”的匹配函数|

最佳答案

看起来您打算将 candidateNames 声明为 vector<string> candidateNames;

vector<string> candidateNames;

void readCandidates ()
{
cin >> nCandidates;
string line;
getline (cin, line);
candidateNames.resize(nCandidates);
for (int i = 0; i < nCandidates; ++i)
{
getline (cin, candidateNames[i]);

}
}

关于c++ - 需要有关 getline 函数错误 : no matching function for call to 'getline(std::istream&, char&)' | 的编译错误的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7574197/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com