我有一个C++作业,需要我创建一个名为Tips的类,该类:

  • 具有单个成员变量float taxRate
  • 有两个构造函数,一个默认值将taxRate设置为.65,一个参数构造器,一个将taxRate设置为用户在我的tipMain.cpp文件中输入的浮点数。
  • 只有一个函数computeTip,它接受两个参数:totBilltipRate,这两个float都必须计算税前餐费,并根据该值和所需的小费率返回小费。

  • 当我尝试在computeTip函数中使用taxRate时,会发生我的问题。

    如果我使用taxRatecomputeTip无法理解taxRate的值,则说明它是未定义的,即使我确实指定了tips::taxRate,甚至在编译Visual Studio状态之前



    我模糊地理解了这个错误,但是即使我在我的.h文件中声明taxRate为静态,我也会收到LNK2019错误。

    Tips.cpp
    #include <iostream>
    #include <float.h>
    #include "Tips.h"
    
    using namespace std;
    
    Tips::Tips()
    {
        taxRate = 0.65;
    }
    
    Tips::Tips(float a)
    {
        taxRate = a;
    }
    
    float computeTip(float totBill, float tipRate)
    {
        float mealOnly = 0;
        mealOnly = (totBill/taxRate);       //written both ways to show errors. taxRate undefined here.
        mealOnly = (totBill/Tips::taxRate); //Error: a nonstatic member reference must be relative to a specific object
        return (mealOnly * tipRate);
    
    }
    

    Tips.h
    #ifndef Tips_H
    #define Tips_H
    #include <float.h>
    
    using namespace std;
    
    class Tips
    {
    public:
        Tips();
        Tips(float);
        float computeTip(float, float, float);
        float taxRate;
    };
    #endif
    

    和我的tipMain.cpp
    #include "Tips.h"
    #include <iostream>
    #include <iomanip>
    
    
    using namespace std;
    
    float tax;
    float meal;
    float tip;
    void tipProcessor();
    
    int main()
    {
        char entry;
        int toggle = 1;
        cout << "Welcome to the Gratuity Calculator!" << endl;
        cout << endl;
        while (toggle != 0)
        {
            cout << "Enter 1 to calculate tip." << endl;
            cout << endl;
            cout << "Enter 2 to exit." << endl;
            cout << endl;
            cout << "Entry: ";
            cin >> entry;
            switch (entry)
            {
            case '1':
                tipProcessor();
                break;
            case '2':
                toggle = 0;
                break;
            default:
                cout << "Enter 1 or 2." << endl;
                cout << endl;
                break;
            }
        }
        system("pause");
        return 0;
    }
    void tipProcessor()
    {
        cout << "Enter bill total: ";
        cin >> meal;
        cout << endl;
        cout << "Enter tax rate: ";
        cin >> tax;
        cout << endl;
        Tips thisTip(tax);
        cout << "Enter tip percent: ";
        cin >> tip;
        cout << endl;
        cout << "Tip is $" << setprecision(4) << thisTip.computeTip(meal, tip, thisTip.taxRate) << "." << endl;
        cout << endl;
    }
    

    最佳答案

    你用

    float computeTip(float totBill, float tipRate)
    

    但是应该是:
    float Tips::computeTip(float totBill, float tipRate)
    

    在执行中。另外,您应该隐藏数据成员。

    关于c++ - 变量未定义,但在构造函数C++中已明确定义,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20247902/

    10-11 18:02