MPNation
MPNation

Reputation: 179

Too many arguments to function

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.

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?

Upvotes: 5

Views: 133787

Answers (4)

Chantola
Chantola

Reputation: 528

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

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

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

Upvotes: 4

Lefsler
Lefsler

Reputation: 1768

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);

Upvotes: 9

megadan
megadan

Reputation: 1833

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

Upvotes: 2

JBentley
JBentley

Reputation: 6260

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:

void printCandidateReport(int candidateNum){

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

Upvotes: 4

Related Questions