2014-09-24 122 views
1

我從我的頭文件中得到這個錯誤:too many arguments to function void printCandidateReport();。我對C++相當陌生,只是需要一些指導來解決這個錯誤。功能太多的參數

我的頭文件是這樣的:

#ifndef CANDIDATE_H_INCLUDED 
#define CANDIDATE_H_INCLUDED 

// Max # of candidates permitted by this program 
const int maxCandidates = 10; 

// How many candidates in the national election? 
int nCandidates; 

// How many candidates in the primary for the state being processed 
int nCandidatesInPrimary; 

// Names of the candidates participating in this state's primary 
extern std::string candidate[maxCandidates]; 

// Names of all candidates participating in the national election 
std::string candidateNames[maxCandidates]; 

// How many votes wone by each candiate in this state's primary 
int votesForCandidate[maxCandidates]; 

void readCandidates(); 
void printCandidateReport(); 
int findCandidate(); 
#endif 

和文件調用這個頭文件:

#include <iostream> 
#include "candidate.h" 
/** 
* 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]); 
     delegatesWon[i] = 0; 
    } 
} 

爲什麼我收到這個錯誤,我該如何解決?

回答

6

在頭文件中聲明:

void printCandidateReport(); 

但在實現是:

void printCandidateReport(int candidateNum){...} 

改變標題文件到

void printCandidateReport(int candidateNum); 
2

錯誤too many arguments to function可以通過消除函數中的多餘參數 (參數)來解決。

發生此錯誤是因爲您的頭文件沒有參數值,並且在實際的源代碼中使用了int參數。

您有兩種選擇,您可以在函數聲明中添加缺少的int參數,或將其從函數中完全刪除。

3

錯誤消息正在告訴你問題是什麼。

在你的頭文件中聲明該函數不帶參數:

void printCandidateReport(int candidateNum){ 

要麼缺少的參數添加到:

void printCandidateReport(); 

在源文件中你int類型的參數來定義它該聲明或將其從定義中刪除。

+0

我從字面上擊敗了你的帖子一秒,奇! – Chantola 2014-09-24 01:31:40

2

頭文件聲明不帶參數的函數printCandidateReport(),cpp文件用int參數定義函數。只是整型參數添加到函數聲明在頭文件來修復它