C++ 函数的参数太多

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26007095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:29:31  来源:igfitidea点击:

Too many arguments to function

c++header

提问by MPNation

I am getting this error from my header file: too many arguments to function void printCandidateReport();. I am fairly new to C++ and just need some guidance in the right direction to solving this error.

我从头文件中收到此错误:too many arguments to function void printCandidateReport();. 我对 C++ 相当陌生,只需要一些正确方向的指导来解决这个错误。

My header file looks like this:

我的头文件如下所示:

#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

and the file calling this header file:

和调用这个头文件的文件:

#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;
    }
}

why am I getting this error and how can I fix it?

为什么我会收到这个错误,我该如何解决?

回答by Lefsler

On the header file you declare:

在您声明的头文件上:

void printCandidateReport ();

But on the implementation is:

但在实现上是:

void printCandidateReport(int candidateNum){...}

Change the header file to

将头文件改为

void printCandidateReport(int candidateNum);

回答by JBentley

The error message is telling you precisely what the problem is.

错误消息准确地告诉您问题是什么。

In your header file you declare the function with no parameters:

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

void printCandidateReport ();

In the source file you define it with a parameter of type int:

在源文件中,您使用类型参数定义它int

void printCandidateReport(int candidateNum){

Either add the missing parameter to the declaration, or remove it from the definition.

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

回答by megadan

The header file declares the function printCandidateReport() with no parameters and the cpp file defines the function with an int parameter. Just add the int parameter to the function declaration in the header file to fix it

头文件声明了没有参数的函数 printCandidateReport(),cpp 文件定义了带有 int 参数的函数。只需在头文件中的函数声明中添加 int 参数即可修复

回答by Chantola

The error too many arguments to functioncan be fixed by eliminating the excess arguments (parameters) in the function .

too many arguments to function可以通过消除函数中多余的参数(参数)来修复错误。

This error occurred because your header file has no parameter values, and in the actual source code you use the intparameter.

发生此错误是因为您的头文件没有参数值,而在实际源代码中您使用了该int参数。

You have two choices, you can add the missing intparameter in the function declaration, or remove it entirely from the function.

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