我有一个带有getline函数的源代码文件,当我编译它时我收到并出错(下面的代码和错误)。我的问题是我从已经编译好的工作程序中复制并粘贴了整个函数(下面也包括在内)。我还在程序中的其他两个源代码文件中有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&amp;,char&amp;)'|
答案 0 :(得分:1)
您希望将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]);
}
}
答案 1 :(得分:0)
candidateNames
是单个字符串,而不是字符串的集合。如何设置它们的矢量?
std::vector<string> candidateNames;
然后在for
循环之前:
candidateNames.resize(nCandidates);
答案 2 :(得分:0)
好像你忘记了
#include <string>
其他标题可能已经拉入字符串类本身,但并非所有支持函数(如getline
重载都接受std::string
。
答案 3 :(得分:0)
对不起,我在发布这个问题时可能不公平。我能够找到问题,它在我的头文件中,我弄乱了那里的变量。 for candidateNames []我忘了头文件末尾的[]。由于我没有在我的帖子中包含该文件,因此您无法看到。谢谢你的帮助。