c++编译器调用错误

C++ Compiler error with calls

本文关键字:错误 调用 编译器 c++      更新时间:2023-10-16

在我的计算机科学课上,我们正在编写一个程序,要求测量4个等腰梯形的高度,底部和顶部,计算周长并确定哪个周长最大。我的程序还没有完成,但是我得到错误,说没有匹配的函数来调用askInput, calcPerimeterfindMax。下面是上下文的代码(抱歉,如果答案是显而易见的,或者我的代码是草率的,这是我第一次上编程课)。

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

声明中的函数签名必须与实现中的签名匹配。这意味着声明必须包含所有需要的函数形参类型。

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

您的函数声明和定义不匹配。像你在定义中所做的那样声明它们,这应该可以工作。

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

这些函数的原型不包括参数。askInput()是不同于askInput(double &, double&, double&, string)的函数

我研究c++已经有一段时间了,但我认为我至少看到了两个问题-

  1. 你需要在顶部声明函数的完整签名。
  2. 至少对于askInput,您正在传递值并取消引用它们。这看起来很奇怪。

希望这对你有帮助!