类型为“ void(*)(int wall)”的C ++参数与类型为“ int”的参数不兼容

时间:2019-10-02 23:55:53

标签: c++ function parameters

我是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和人工费用。了解如何解决第一个问题,因为本质上是相同的问题,我可以修复所有三个问题。

1 个答案:

答案 0 :(得分:1)

#include <iostream>

using namespace std;

void hours(int gallons);

int gallons(int wall) 
{
    return wall * wall;
}

void hours(int gallons)

{
    int hours;

    hours = gallons * 8;

    cout << "Hours of labor required: " << hours << endl;
}

int main()
{
    int wall;
    int pricePaint;

    cout << "Enter square feet of wall: ";
    cin >> wall;

    cout << "Enter price of paint per gallon: ";
    cin >> pricePaint;

    hours(gallons(wall));

    return 0;

}

您可能需要这种代码。

在您的代码中,主函数中的'gallons'被视为函数,因为'gallons'是函数名称。

但是,您不想使用函数作为参数,而是函数的返回值。

所以您只需要修复上面的代码即可。