我正在使用 Qt Creator 来尝试创建一个基本的计算器应用程序。我试图测试前几个方法并且必须编写一个以使其余的编码更容易,但是,该方法没有编译。我正在尝试访问一个包含计算器屏幕当前值的静态变量,但它不断给我:
C:\Users\****\Documents\Qt Projects\SimpleCalculator\calculator.cpp:15: error: C2248: 'Calculator::currVal' : cannot access private member declared in class 'Calculator'
这是Calculator.cpp
#include "calculator.h"
#include <QLCDNumber>
Calculator::Calculator(QWidget *parent) :
QWidget(parent), ui(new Ui::Calculator)
{
ans = 0;
currVal = 0;
setupUi(this);
}
//problem method
QString getNewVal(qint64 nextDig)
{
//--------------------------------------------
long long int val = Calculator::currVal;//this is where I am trying to access the variable
//--------------------------------------------
if(nextDig==0)
{
if(val > 0)
{
QString str = QString::number(val);
str.append("0");
return str;
}
else
{
return "0";
}
}
else if(nextDig==1)
{
QString str = QString::number(val);
str.append("1");
return str;
}
return NULL;
}
void Calculator::on_Zero_clicked()
{
ui->Display->display(getNewVal(0));
currVal = ui->Display->intValue();
}
void Calculator::on_One_clicked()
{
}
这是头文件:
#ifndef CALCULATOR_H
#define CALCULATOR_H
#include "ui_calculator.h"
class Calculator : public QWidget, private Ui::Calculator
{
Q_OBJECT
public:
Calculator(QWidget *parent = 0);
private slots:
void on_Zero_clicked();
void on_One_clicked();
private:
Ui::Calculator *ui;
QString getNewVal(quint64);
static long long int ans;
static long long int currVal;
};
long long int Calculator::ans = 0;
long long int Calculator::currVal = 0;
#endif
// CALCULATOR_H