C++ 标识符未定义

C++ identifier is undefined

我是 C++ 的新手,我不明白为什么会收到此错误。在 5 个相似的语句中,有 3 个标记错误,但其他两个没问题。错误在主函数中。

    #include <iostream>
using namespace std;

// Function declaration
void getGallons(int wall);
void getHours(int gallons);
void getCostpaint(int gallons, int pricePaint);
void getLaborcharges(int hours);
void getTotalcost(int costPaint, int laborCharges);

// Function definition
void getGallons(int wall)
{
    int gallons;

    gallons = wall / 112;

    cout << "Number of gallons of paint required: " << gallons << endl;


}

// Function definition
void getHours(int gallons)
{
    int hours;

    hours = gallons * 8;

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


}

// Function definition
void getCostpaint(int gallons, int pricePaint)
{
    int costPaint;

    costPaint = gallons * pricePaint;

    cout << "The cost of paint: " << costPaint << endl;
}

// Function definition
void getLaborcharges(int hours)
{
    int laborCharges;

    laborCharges = hours * 35;

    cout << "The labor charge: " << laborCharges << endl;
}

// Funtion definition
void getTotalcost(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;

    getGallons(wall);

    getHours(gallons); // error here

    getCostpaint(gallons, pricePaint);

    getLaborcharges(hours); // error here

    getTotalcost(costPaint, laborCharges); //error here

    return 0;

}

本课的重点是在代码中使用函数和传递参数。我不应该使用全局变量。如果您有更好的方法来做到这一点,请分享。

减少到三行(其他错误类似):

int wall;    
getGallons(wall);
getHours(gallons); // error here

虽然 wall 已定义,但 gallons 未定义。无论如何,你想从哪里得到 gallons?结果隐藏在另一个函数的深处。你想怎么把它从那里弄出来?

嗯,你需要一个 return 值:

  int getGallons(int wall)
//^^^ !
{
     int gallons = wall / 112;
     // ...
     return gallons; // !
}

这样,您可以像这样使用函数:

int gallons = getGallons(wall);
// now gallons is defined and you can use it:
getHours(gallons);

其他函数和变量类似。

通常,不是在同一个函数中混合逻辑(计算)和输出的好主意。所以我宁愿将控制台写入 main 函数:

int getGallons(int wall) { return wall / 112; }
int getHours(int gallons) { return gallons * 8; }

int wall;
std::cin >> wall;
int gallons = getGallons(int wall);
std::cout << ...;
int hours = getHours(gallons);
std::cout << ...;

注意了吗?所有 input/output 现在处于同一级别...

旁注:如果您在定义之前不使用函数,则无需在定义之前声明函数:

//void f(); // CAN be ommitted
void f() { };
void g() { f(); }

反例:

void f();
void g() { f(); } // now using f before it is defined, thus you NEED do declare it
void f() { };

如果你仍然想保留声明是一个风格问题(但在管理不同编译单元中的代码时会变得很重要,因为你会在头文件中有声明——你很快就会遇到下一课)。

原因是变量在使用前没有定义。 以下更改已添加到代码中。

  • 因为您将函数命名为 "getSomeValue()" 最好使用 return 类型而不是 void。
  • 最好用double而不是int,因为计算中有除法
  • 还使用嵌套函数调用来减少代码行数。

固定代码:

#include <iostream>
using namespace std;

// Function declaration
int getGallons(int wall);
int getHours(int gallons);
int getCostpaint(int gallons, int pricePaint);
int getLaborcharges(int hours);
int getTotalcost(int costPaint, int laborCharges);

// Function definition
int getGallons(int wall)
{
    int gallons;

    gallons = wall / 112;

    cout << "Number of gallons of paint required: " << gallons << endl;
    return gallons;
}

// Function definition
int getHours(int gallons)
{
    int hours;

    hours = gallons * 8;

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

// Function definition
int getCostpaint(int gallons, int pricePaint)
{
    int costPaint;

    costPaint = gallons * pricePaint;

    cout << "The cost of paint: " << costPaint << endl;
    return costPaint;
}

// Function definition
int getLaborcharges(int hours)
{
    int laborCharges;

    laborCharges = hours * 35;

    cout << "The labor charge: " << laborCharges << endl;
    return laborCharges;
}

// Funtion definition
int getTotalcost(int costPaint, int laborCharges)
{
    int totalCost;

    totalCost = costPaint + laborCharges;

    cout << "The total cost of the job: " << totalCost << endl;
    return totalCost;
}

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

    int costPaint = getCostpaint(getGallons(wall), pricePaint);
    int laborCharges = getLaborcharges(getHours(getGallons(wall)));
    getTotalcost(costPaint, laborCharges);

    return 0;

}

这里有几个errors/issues

  1. 您的函数声明是多余的。如果您计划在定义之前调用函数,则只需要它们。

  2. 在您的主要方法中,您没有声明加仑

  3. 在您的 main 方法中,您没有为 wall 和 pricepaint 赋值。

  4. 在您的函数中,您通过副作用进行操作,这意味着您打印到控制台,而不是返回任何内容。