【发布时间】:2019-10-02 23:55:53
【问题描述】:
我对 C++ 很陌生,我不确定如何纠正错误。这是我的代码的一部分,错误出现在我的主函数中。
#include <iostream>
using namespace std;
// Function declaration
void gallons(int wall);
void hours(int gallons);
void costPaint(int gallons, int pricePaint);
void laborCharges(int hours);
void totalCost(int costPaint, int laborCharges);
// Function definition
void gallons(int wall)
{
int gallons;
gallons = wall / 112;
cout << "Number of gallons of paint required: " << gallons << endl;
}
// Function definition
void hours(int gallons)
{
int hours;
hours = gallons * 8;
cout << "Hours of labor required: " << hours << endl;
}
// Function definition
void costPaint(int gallons, int pricePaint)
{
int costPaint;
costPaint = gallons * pricePaint;
cout << "The cost of paint: " << costPaint << endl;
}
// Function definition
void laborCharges(int hours)
{
int laborCharges;
laborCharges = hours * 35;
cout << "The labor charge: " << laborCharges << endl;
}
// Funtion definition
void totalCost(int costPaint, int laborCharges)
{
int totalCost;
totalCost = costPaint + laborCharges;
cout << "The total cost of the job: " << totalCost << endl;
}
// The main method
int main()
{
int wall;
int pricePaint;
cout << "Enter square feet of wall: ";
cin >> wall;
cout << "Enter price of paint per gallon: ";
cin >> pricePaint;
gallons(wall);
hours(gallons); // here's where the error is
costPaint(gallons, pricePaint); // here's where the error is
laborCharges(hours); // here's where the error is
return 0;
}
这是我不断收到错误“void(*)(int wall) 类型的 C++ 参数与“int”类型的参数不兼容的地方“我得到它的小时数、costPaint 和人工费用。如果我能弄清楚找出如何解决第一个问题,我可以解决所有三个问题,因为它本质上是相同的问题。
【问题讨论】:
-
gallons(wall);-- 你对这行代码的意图是什么? -
在
laborCharges(hours);,你认为hours是什么意思? -
你的
gallons函数在哪里? -
gallons是变量还是函数?语句gallons(wall)表示gallons是一个函数,而hours(gallons)表示gallons是一个变量。更复杂的是,您没有gallons的变量声明或函数声明。 -
你需要清楚地区分变量和函数。您已将
hours定义为两者(您在函数void hours(int gallons)中定义了一个变量int hours;,这是非法的,但它令人困惑且是个坏主意)。你想让你的hours()函数返回一个值吗?如果是这样,您需要将其定义为int hours(int gallons)。
标签: c++ function parameters