简体   繁体   中英

C++ Compiler error with calls

In my Computer Science class we are making a program that asks for the height, bottom, and top measurements of 4 isosceles trapezoids, calculates the perimeter and determines which has the largest perimeter. My program isn't complete yet, but I am getting errors saying that there is no matching function to call for askInput , calcPerimeter , and findMax . Here is the code for context (Sorry if the answer is obvious, or my code is sloppy, this is the first programming class I have taken).

#include <iostream>
#include <string>

using namespace std;

void intro();
void askInput();
double calcPerimeter();
double findMax();
double highest;

int main()
{
    intro();

    double t1, b1, h1, p1;  //Top, bottom, height, and perimiter of first trapezoid
    double t2, b2, h2, p2,
           t3, b3, h3, p3,
           t4, b4, h4, p4;  //other four trapezoids
    double max;             //largest perimeter

    askInput(t1, b1, h1, "first");
    askInput(t2, b2, h2, "second");
    askInput(t3, b3, h3, "third");
    askInput(t4, b4, h4, "fourth");

    p1 = calcPerimeter(t1, b1, h1);
    p2 = calcPerimeter(t2, b2, h2);
    p3 = calcPerimeter(t3, b3, h3);
    p4 = calcPerimeter(t4, b4, h4);

    max = findMax(p1, p2, p3, p4);

    cout << "Results" << endl;
    cout << "\tFirst: \tTop:" << t1 << "\tBottom: " << b1 << "\tHeight: " << h1 << "\tPerimeter: " << p1 << endl;
    cout << "\tSecond: \tTop:" << t2 << "\tBottom: " << b2 << "\tHeight: " << h2 << "\tPerimeter: " << p2 << endl;
    cout << "\tThird: \tTop:" << t3 << "\tBottom: " << b3 << "\tHeight: " << h3 << "\tPerimeter: " << p3 << endl;
    cout << "\tFourth: \tTop:" << t4 << "\tBottom: " << b4 << "\tHeight: " << h4 << "\tPerimeter: " << p4 << endl;
    cout << endl << "\tLargest Perimeter: " << highest << endl;


    system("pause");
    return 0;
}

void intro()
{
    cout << "Lab G: Trapezoid with largest perimeter" << endl;
    cout << "---------------------------------------" << endl; \
    cout << "This program will calculate the perimeter of four trapezoids." << endl;
    cout << "You will have to enter the top, bottom, and height of each trapezoid." << endl;
    cout << "The program will then find the trapezoid with the largest perimeter and output it." << endl;
}

void askInput(double &top, double &bottom, double &height, string whichT)
{
    cout << "Enter values for the " << whichT << " trapezoid." << endl;
    cout << "\tTop: ";
    cin >> top;
    cout << "\tBottom: ";
    cin >> bottom;
    cout << "\tHeight: ";
    cin >> height;
}

double calcPerimeter(double top, double bottom, double height)
{
    double answer;

    //some calculations

    return answer;
}

double findMax(double a, double b, double c, double d)
{
    double highest;
    highest = a;
    if (b > highest)
    {
        highest = b;
    }
    if (c > highest)
    {
        highest = c;
    }
    if (d > highest)
    {
        highest = d;
    }
    return highest;
}

The function signature in the declaration has to match the signature in the implementation. This means that the declaration has to contain all the needed types of the function parameters.

void askInput(double &top, double &bottom, double &height, string whichT);

double calcPerimeter(double top, double bottom, double height);

double findMax(double a, double b, double c, double d);

Your prototypes for those functions don't include the parameters. askInput() is a different function from askInput(double &, double&, double&, string) .

It's been a while since I worked on C++, but I think I see at least 2 issues -

  1. You need to declare the functions at the top with their full signature.
  2. At least for askInput you are passing in the values and the de-referencing them. This looks strange to me.

Hope this helps!

Your declaration and definition of function do not match . Declare them as you have done in your definition and this should work.

 void askInput(); --> 
 void askInput(double &, double &, double &, string );

 double calcPerimeter();-->
 double calcPerimeter(double & , double & , double& );

 double findMax();-->
 double findMax(double &, double &, double &, double &);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM