Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 参数太多,无法正常工作_C++_Header - Fatal编程技术网

C++ 参数太多,无法正常工作

C++ 参数太多,无法正常工作,c++,header,C++,Header,我从头文件中得到此错误:函数void printCandidateReport()的参数太多。对于C++,我是相当新的,只需要在正确的方向上指导解决这个错误。 我的头文件如下所示: #ifndef CANDIDATE_H_INCLUDED #define CANDIDATE_H_INCLUDED // Max # of candidates permitted by this program const int maxCandidates = 10; // How many candidat

我从头文件中得到此错误:
函数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;
    }
}
#包括
#包括“候选者.h”
/**
*找到具有指定名称的候选人。返回数组索引
*对于已找到的候选项,如果找不到,则取消指定。
*/
int findCandidate(标准::字符串名称){
int result=nCandidates;
对于(int i=0;i=需要时)

cout通过消除多余的参数,可以修复函数中参数过多的错误

函数中的(参数)

发生此错误的原因是头文件没有参数值,并且在实际源代码中使用了
int
参数


您有两种选择,可以在函数声明中添加缺少的
int
参数,也可以将其完全从函数中删除。

错误消息准确地告诉您问题所在

在头文件中,您声明了不带参数的函数:

void printCandidateReport ();
在源文件中,使用类型为
int
的参数定义它:

void printCandidateReport(int candidateNum){

将缺少的参数添加到声明中,或将其从定义中删除。

头文件声明函数printCandidateReport()不带参数,cpp文件使用int参数定义函数。只需将int参数添加到头文件中的函数声明中,即可修复您声明的头文件上的int参数:

void printCandidateReport ();
但在实施方面:

void printCandidateReport(int candidateNum){...}
将头文件更改为

void printCandidateReport(int candidateNum);

我真的比你的帖子快了一秒钟,真奇怪!