[英]need help with this compile error for getline function error: no matching function for call to 'getline(std::istream&, char&)'|
I have a source code file with getline function in it and I receive and error when I compile it (code and error below). 我有一个带有getline函数的源代码文件,我在编译该文件时会收到错误消息(下面的代码和错误)。 My problem is that I copied and pasted the whole function from an already compiled and working program (included below as well). 我的问题是我从一个已经编译且可以运行的程序(也包括在下面)中复制并粘贴了整个函数。 I also have the getline function in 2 other source code files in the program, both of which compile fine. 我在程序中的其他2个源代码文件中也都有getline函数,它们都可以很好地编译。 I am fairly new to programming and just started c++ programing (much better at Java) so try to keep answers simple. 我对编程还很陌生,所以刚开始进行c ++编程(Java更好),因此请尝试使答案保持简单。 I looked through some of the already posted questions and answers here (and google) but all the ones I found the answers say that the parameters for the function are not correct. 我在这里(和google)浏览了一些已经发布的问题和答案,但是所有发现问题的答案都说该函数的参数不正确。 But I know they are correct here because it works in the other program just fine. 但是我知道它们在这里是正确的,因为它可以在其他程序中正常工作。 Also as you can see in the working file the only #included is iostream and it works, that's with g++ complier in Code::Blocks. 您还可以在工作文件中看到,唯一包含的#include是iostream,它可以工作,这与Code :: Blocks中的g ++编译器兼容。 I made sure to include all the variables/constants needed as well. 我确保也包括所有需要的变量/常量。
Here is the code for the function and file I am getting the error from. 这是我得到错误的函数和文件的代码。 The parts I am refering to are marked by 3 * (sorry best I could do). 我所指的部分标有3 * (对不起,我能做的最好)。 The error is reprinted at the bottom of post as well. 错误也会在帖子底部重新打印。
#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;
}
}
here is the code from the already working program. 这是已经运行的程序中的代码。
#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;
}
error: no matching function for call to 'getline(std::istream&, char&)'| 错误:没有匹配的函数可以调用'getline(std :: istream&,char&)'|
Looks like you intended candidateNames to be declared as a vector<string> 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]);
}
}
candidateNames
is a single string, not a collection of strings. candidateNames
是单个字符串,而不是字符串的集合。 What about setting up a vector of them? 如何设置它们的向量?
std::vector<string> candidateNames;
And then before your for
loop: 然后在for
循环之前:
candidateNames.resize(nCandidates);
Looks like you forgot to 好像你忘了
#include <string>
It's likely that the other headers have pulled in the string class itself, but not all the supporting functions such as getline
overloads that accept std::string
. 其他标头可能已经拉入了字符串类本身,但并非所有支持功能,例如接受std::string
getline
重载。
Sorry I might have been unfair when posting this question. 抱歉,发布此问题可能不公平。 I was able to find the problem, it was in my header file, I messed up the variables there. 我能够找到问题,它在我的头文件中,我弄乱了那里的变量。 for candidateNames[] I forgot the [] at the end in the header file. 对于候选人名称[]我忘记了头文件末尾的[]。 And since I didn't include that file in my post you couldn't see that. 而且由于我没有在我的帖子中包含该文件,所以您看不到它。 Thanks for the help though. 谢谢您的帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.