繁体   English   中英

调用的C ++编译器错误

[英]C++ Compiler error with calls

在我的计算机科学课上,我们正在制作一个程序,要求对4个等腰梯形的高度,底部和顶部进行测量,计算周长并确定哪个周长最大。 我的程序还没有完成,但是我收到错误消息,说没有匹配函数可以调用askInputcalcPerimeterfindMax 这是上下文代码(很抱歉,如果答案很明显,或者我的代码草率,这是我参加的第一个编程类)。

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

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

自从我从事C ++以来已经有一段时间了,但是我认为我至少看到了两个问题-

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

希望这可以帮助!

您的声明和函数定义不匹配。 像定义中一样声明它们,这应该起作用。

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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