Te veel argumenten om te functioneren

Ik krijg deze fout van mijn headerbestand: too many arguments to function void printCandidateReport();. Ik ben vrij nieuw in C++ en heb alleen wat begeleiding nodig in de goede richting om deze fout op te lossen.

Mijn headerbestand ziet er als volgt uit:

#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

en het bestand dat dit headerbestand aanroept:

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

waarom krijg ik deze foutmelding en hoe kan ik deze oplossen?


Antwoord 1, autoriteit 100%

In het headerbestand geeft u aan:

void printCandidateReport ();

Maar over de implementatie is:

void printCandidateReport(int candidateNum){...}

Verander het headerbestand in

void printCandidateReport(int candidateNum);

Antwoord 2, autoriteit 50%

De foutmelding vertelt je precies wat het probleem is.

In je headerbestand declareer je de functie zonder parameters:

void printCandidateReport ();

In het bronbestand definieert u het met een parameter van het type int:

void printCandidateReport(int candidateNum){

Voeg de ontbrekende parameter toe aan de declaratie of verwijder deze uit de definitie.


Antwoord 3, autoriteit 25%

Het headerbestand declareert de functie printCandidateReport() zonder parameters en het cpp-bestand definieert de functie met een int-parameter. Voeg gewoon de parameter int toe aan de functiedeclaratie in het headerbestand om het te repareren


Antwoord 4, autoriteit 25%

De fout too many arguments to functionkan worden verholpen door de overtollige argumenten te verwijderen
(parameters) in de functie .

Deze fout is opgetreden omdat uw headerbestand geen parameterwaarden heeft en u in de eigenlijke broncode de parameter intgebruikt.

Je hebt twee keuzes, je kunt de ontbrekende parameter inttoevoegen aan de functiedeclaratie of deze volledig uit de functie verwijderen.

Other episodes