2011-09-27 173 views
0

我有一個帶有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的&,炭&)' 不匹配函數|

+3

真的,這是你能做的最好的?如何發佈5行代碼具有相同的問題? –

+0

我把所有的代碼放在那裏試圖幫助理解這個問題,並且清除我知道的問題並不是問題,比如#include 類似的東西,我已經嘗試過了,並且它不起作用。 – gimpdogg

回答

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

    } 
} 
+0

我不打算成爲一個矢量,它可以在其他程序中正常工作。我可以嘗試改變它,但我想知道爲什麼它在另一個程序中運行,而不是在這之前運行。 – gimpdogg

+0

@gimpdogg函數的readCandidates()的整個邏輯以及名稱(readCandidates)意味着它應該讀取幾個候選名稱,因此是一個向量。你也在你的工作示例中使用數組'std :: string candidateNames [maxCandidates];'。矢量是對數組的改進。 –

+0

好的,我明白了,謝謝,但我確實已經找到了問題。這是在我的頭文件中,我忘記了候選名字後面的[]。 – gimpdogg

0

candidateNames是一個字符串,而不是字符串的集合。如何設置它們的矢量呢?

std::vector<string> candidateNames;

然後你for循環之前:

candidateNames.resize(nCandidates);

+0

我從來沒有做過一個矢量,這也是家庭作業的一部分,這就是爲什麼我已經工作的程序(單個文件)。我們假設只是將函數分離到其他源文件中,這些變量也用於其他文件中。我可以嘗試,但我不知道這將如何影響程序的其他部分,通常這些作業我們不必像這樣(通常)修改它們。此外,爲什麼它會在已經運行的程序中編譯好(在我嘗試將所有代碼改爲載體之前)? – gimpdogg

0

看起來你忘了

#include <string> 

這可能是因爲其他的頭都在串類本身拉,但並不是所有的支持功能,例如getline重載接受t std::string

+0

加了#include 得到同樣的錯誤。 – gimpdogg

0

對不起,我發佈這個問題可能是不公平的。我能夠找到問題,它在我的頭文件中,我弄亂了那裏的變量。對於candidatesNames [],我忘記了頭文件末尾的[]。由於我沒有在我的文章中包含該文件,因此您無法看到該文件。感謝您的幫助。

相關問題