我有一個帶有getline函數的源代碼文件,當我編譯它(下面的代碼和錯誤)時,我收到併發生錯誤。我的問題是,我複製並粘貼了已編譯和正在運行的程序中的全部功能(也包括在下面)。我在程序中的其他兩個源代碼文件中也有getline函數,兩者都很好地編譯。我對編程相當陌生,剛開始編寫C++程序(在Java中更好),所以儘量保持答案簡單。我查看了一些已經發布的問題和答案(和谷歌),但所有我找到答案的人都說函數的參數不正確。但我知道他們在這裏是正確的,因爲它在其他程序中工作得很好。也正如你在工作文件中看到的那樣,唯一的#included是iostream,它的工作原理就是在Code :: Blocks中使用g ++編譯器。我確保也包含所有需要的變量/常量。getline函數錯誤需要此編譯錯誤的幫助:沒有匹配函數調用'getline(std :: istream&,char&)'|
這是我從中獲取錯誤的函數和文件的代碼。我所指的部分標有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的&,炭&)' 不匹配函數|
真的,這是你能做的最好的?如何發佈5行代碼具有相同的問題? –
我把所有的代碼放在那裏試圖幫助理解這個問題,並且清除我知道的問題並不是問題,比如#include類似的東西,我已經嘗試過了,並且它不起作用。 –
gimpdogg