我从头文件中收到此错误: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;
}
}
为什么我会收到此错误,我该如何解决?
答案 0 :(得分:7)
在头文件中声明:
void printCandidateReport ();
但实施是:
void printCandidateReport(int candidateNum){...}
将标题文件更改为
void printCandidateReport(int candidateNum);
答案 1 :(得分:3)
错误消息正好告诉您问题所在。
在头文件中,声明没有参数的函数:
void printCandidateReport ();
在源文件中,您使用int
类型的参数定义它:
void printCandidateReport(int candidateNum){
将缺少的参数添加到声明中,或将其从定义中删除。
答案 2 :(得分:2)
错误too many arguments to function
可以通过消除多余的参数来解决
函数中的(参数)。
发生此错误是因为您的头文件没有参数值,而在实际源代码中您使用的是int
参数。
您有两个选择,您可以在函数声明中添加缺少的int
参数,或者从函数中完全删除它。
答案 3 :(得分:2)
头文件声明函数printCandidateReport()没有参数,cpp文件使用int参数定义函数。只需将int参数添加到头文件中的函数声明中即可修复它