变量未定义,但在我的构造函数C ++中明确定义

时间:2013-11-27 16:30:15

标签: c++ static lnk2019

我有一个C ++任务,要求我创建一个名为Tips的类,即:

  • 只有一个成员变量float taxRate
  • 有两个构造函数,一个将taxRate设置为.65的默认值,以及一个参数构造函数,它将taxRate设置为用户在tipMain.cpp文件中输入的任何浮点数。
  • 只有一个函数computeTip,它接受​​两个参数:totBilltipRate,两个float s必须计算税前餐费用,并返回该提示基于该值和所需的提示率。

当我尝试在computeTip函数中使用taxRate时,会出现问题。

如果我使用taxRate,则computeTip无法理解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;
}

4 个答案:

答案 0 :(得分:3)

您使用

float computeTip(float totBill, float tipRate)

但它应该是:

float Tips::computeTip(float totBill, float tipRate)

在实施中。你也应该隐藏你的数据成员。

答案 1 :(得分:0)

您将computeTip()定义为全局函数,因此只能进行公共静态成员访问。定义为float Tips::computeTip(float ...),它应该编译。

答案 2 :(得分:0)

您尚未指定computeTipTips的方法。将其更改为:

float Tips::computeTip(float totBill, float tipRate)
{
    float mealOnly = 0;
    mealOnly = (totBill/taxRate);
    return (mealOnly * tipRate);
}

一切都应该好。

答案 3 :(得分:0)

您的方法声明和方法定义没有相同的签名。在头文件中,您指出computeTip应该使用3个float个参数,但是当您在.cpp文件中定义它时,它只需要两个参数。

这是对其他用户建议的补充,为函数添加前缀Tips::以表明它是Tips类的一部分,因此应该能够访问成员变量,例如{{1 }}

因此,您的标头声明应如下所示:

taxRate

您的方法定义应该类似于

float computeTips(float, float);
相关问题