[英]C++ Compiler error with calls
在我的計算機科學課上,我們正在制作一個程序,要求對4個等腰梯形的高度,底部和頂部進行測量,計算周長並確定哪個周長最大。 我的程序還沒有完成,但是我收到錯誤消息,說沒有匹配函數可以調用askInput
, calcPerimeter
和findMax
。 這是上下文代碼(很抱歉,如果答案很明顯,或者我的代碼草率,這是我參加的第一個編程類)。
#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 ++以來已經有一段時間了,但是我認為我至少看到了兩個問題-
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.