"Too Many Initializer Values" in C++ function parameters

10,266

Solution 1

This

double calculateCost(itemPrice, itemQuantity, itemDiscount);

is a declaration of a scalar object of the type double with the name of identifier calculateCost. Remove the type specifier

calculateCost(itemPrice, itemQuantity, itemDiscount);

Also take into account that the function shall have a return statement with an expression.

Solution 2

When you use

double calculateCost(itemPrice, itemQuantity, itemDiscount);

the compiler thinks you are trying to declare a function local variable named calculateCost. Such a variable cannot be initialized by the contents within the parenthesis. To make the function call and capture the return value, use

double cost = calculateCost(itemPrice, itemQuantity, itemDiscount);
Share:
10,266

Related videos on Youtube

Frazer Zero
Author by

Frazer Zero

Updated on June 04, 2022

Comments

  • Frazer Zero
    Frazer Zero almost 2 years

    This, "Too Many Initializer Values", error keeps occurring every time I try to pass by reference with these variables:

    Main file. This is where the error occurs and where I'm having all of the trouble.

    #include <iostream>
    #include <fstream>
    #include "Header.h"
    using namespace std;
    
    int main() {
    
        std::string itemName;
        double itemPrice;
        double itemQuantity;
        double itemDiscount;
    
        //Opening input file
        ifstream infile;
        infile.open("InputFile.txt");
    
        infile >> itemName;
        infile >> itemPrice;
        infile >> itemQuantity;
        infile >> itemDiscount;
    
        //Opening output file
        ifstream outfile;
        outfile.open("OutputFile.txt");
    
        //This is where the error occurs, a red underline at "itemQuantity"
        double calculateCost(itemPrice, itemQuantity, itemDiscount);
    }
    

    Function definition. Here you can see I'm trying to use "Pass By Reference".

    #include <iostream>
    
    double calculateCost(double &price, double &quantity, double &discountPct) {
    
        //Placeholder code
        std::cout << "Hello world.\n";
    }
    

    Header. I included the "&" symbols for the parameters here as well.

    #pragma once
    
    double calculateCost(double &price, double &quantity, double &discountPct);
    
    • NathanOliver
      NathanOliver over 4 years
      Voting to close as a typo. Get rid of the return type in double calculateCost(itemPrice, itemQuantity, itemDiscount);. i.e calculateCost(itemPrice, itemQuantity, itemDiscount);