getline 함수가 포함 된 소스 코드 파일을 받았는데 컴파일하고받을 때 오류가 발생했습니다 (아래 코드 및 오류). 내 문제는 내가 복사 한 이미 컴파일 된 및 작업 프로그램 (아래뿐만 아니라 포함)에서 전체 함수를 붙여 넣은 것입니다. 나 또한 프로그램에서 두 개의 다른 소스 코드 파일에 getline 함수가 있으며, 둘 다 잘 컴파일됩니다. 나는 프로그래밍에 익숙하지 않고 C++ 프로그래밍을 시작했다. (Java의 경우 훨씬 더 좋다.) 그래서 간단한 대답을 유지하려고 노력한다. 이미 게시 된 질문 및 답변 (여기서는 Google)을 살펴 보았지만 답변의 매개 변수가 올바르지 않다고 답한 모든 것을 보았습니다. 그러나 다른 프로그램에서 잘 작동하기 때문에이 파일들이 정확하다는 것을 알고 있습니다. 또한 작업 파일에서 볼 수 있듯이 #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 (표준 : : IStream을 &, 문자 &)'을 호출 일치 기능 | 당신이 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]);
}
}
정말 그게 최선입니다. 동일한 문제가있는 5 행 코드를 게시하는 방법은 어떻습니까? –
나는 문제를 이해하는 데 도움이되는 모든 코드를 넣었고 #include과 같은 문제는 내가 알지 못하는 것들을 제거하는데 도움을 주었다. 이미 시도했지만 작동하지 않았다. –
gimpdogg