2011-09-27 2 views
0

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]); 

    } 
} 
+3

정말 그게 최선입니다. 동일한 문제가있는 5 행 코드를 게시하는 방법은 어떻습니까? –

+0

나는 문제를 이해하는 데 도움이되는 모든 코드를 넣었고 #include 과 같은 문제는 내가 알지 못하는 것들을 제거하는데 도움을 주었다. 이미 시도했지만 작동하지 않았다. – gimpdogg

답변

1

보인다. 벡터를 설정하는 것은 어떻습니까?

std::vector<string> candidateNames;

다음 for 루프 전에 : 당신이 다른 헤더 문자열 클래스 자체에서 가져온 것으로

#include <string> 

그것은 가능성을 잊었처럼

candidateNames.resize(nCandidates);

+0

나는 그것이 벡터가되도록 의도하지는 않았지만 다른 프로그램에서도 그렇습니다. 나는 그것을 바꾸려고 노력할 수있다. 그러나 나는 그것이하기 전에 다른 프로그램에서 왜이 프로그램이 아닌지를 알고 싶다. – gimpdogg

+0

@gimpdogg 함수의 readCandidates()와 이름 (readCandidates)의 전체 논리는 몇 가지 후보 이름, 즉 벡터를 읽어야 함을 의미합니다. 또한 작업 예제에서'std :: string candidateNames [maxCandidates];'배열을 사용하고 있습니다. 벡터는 배열을 개선 한 것입니다. –

+0

알았어, 고마워.하지만 이미 문제를 발견 했어. 그것은 내 헤더 파일에 있었고 후보자 이름 뒤에 []을 (를) 깜빡했습니다. – gimpdogg

0

candidateNames로 단일 문자열이 아닌 문자열의 집합입니다 선언하기위한 것처럼

+0

나는 벡터를 한 번도 해보지 않았으며, 숙제의 일부이기도하다. 그래서 나는 이미 작동하는 프로그램 (단일 파일)을 가지고있다. 우리는 함수를 다른 소스 파일로 분리한다고 가정하고 있습니다.이 변수는 다른 파일에서도 사용됩니다. 시도해 볼 수는 있지만 이것이 프로그램의 다른 부분에 어떻게 영향을 미치는지 일반적으로 알지 못합니다. 일반적으로 숙제를 수정할 필요가 없습니다. 또한 왜 내가 이미 벡터로 변경하려고 시도하기 전에 이미 작동중인 프로그램에서 제대로 컴파일 될 수 있습니까? – gimpdogg

0

이 보이는 , 그러나 모든 보조 기능이 getline과 같이 accep에 과부하되는 것은 아닙니다. t std::string.

+0

에 #include 같은 오류가 발생합니다. – gimpdogg

0

죄송합니다.이 질문을 게시 할 때 불편을 끼쳐 드려 죄송합니다. 나는 문제를 발견 할 수 있었다, 그것은 나의 헤더 파일에 있었고, 나는 변수를 엉망으로 만들었다. for candidateNames [] 나는 헤더 파일의 끝에 []를 잊었다. 그리고 그 파일을 내 게시물에 포함시키지 않았으므로 그 파일을 볼 수 없었습니다. 그래도 도움을 주셔서 감사합니다.

관련 문제