我得到“'Operation'不是一个类或命名空间”(mainwindow.cpp文件)在我使用Operation :: ADD,Operation :: SUBTRACT,Operation :: MULTIPLY或Operation :: DIVIDE的每一行中。
main.cpp中:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.setWindowTitle("Calculator");
w.resize(300, 475);
w.show();
return a.exec();
}
operationenum.h:
#ifndef OPERATIONENUM_H
#define OPERATIONENUM_H
enum class Operation
{
ADD,
SUBTRACT,
MULTIPLY,
DIVIDE,
MODULO
};
#endif // OPERATIONENUM_H
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QLabel>
#include "operationenum.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
QLabel* text;
void cancel();
void plusMinusClicked();
void modulo();
void add();
void subtract();
void multiply();
void divide();
void equals();
void dotBtnClicked();
void _0BtnClicked();
void _1BtnClicked();
void _2BtnClicked();
void _3BtnClicked();
void _4BtnClicked();
void _5BtnClicked();
void _6BtnClicked();
void _7BtnClicked();
void _8BtnClicked();
void _9BtnClicked();
long double number1;
long double number2;
Operation operation;
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPushButton>
#include <string>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
text = new QLabel("42", this);
// Operation buttons.
QPushButton* addBtn = new QPushButton("+", this);
QPushButton* subtractBtn = new QPushButton("-", this);
QPushButton* multiplyBtn = new QPushButton("x", this);
QPushButton* divideBtn = new QPushButton("÷", this);
QPushButton* equalsBtn = new QPushButton("=", this);
QPushButton* cancelBtn = new QPushButton("C", this);
QPushButton* plusMinusBtn = new QPushButton("±", this);
QPushButton* moduloBtn = new QPushButton("%", this);
// Digit buttons.
QPushButton* dotBtn = new QPushButton(".", this);
QPushButton* _0Btn = new QPushButton("0", this);
QPushButton* _1Btn = new QPushButton("1", this);
QPushButton* _2Btn = new QPushButton("2", this);
QPushButton* _3Btn = new QPushButton("3", this);
QPushButton* _4Btn = new QPushButton("4", this);
QPushButton* _5Btn = new QPushButton("5", this);
QPushButton* _6Btn = new QPushButton("6", this);
QPushButton* _7Btn = new QPushButton("7", this);
QPushButton* _8Btn = new QPushButton("8", this);
QPushButton* _9Btn = new QPushButton("9", this);
text -> setGeometry(0, 0, 300, 100);
cancelBtn -> setGeometry(0, 100, 75, 75);
plusMinusBtn -> setGeometry(75, 100, 75, 75);
moduloBtn -> setGeometry(150, 100, 75, 75);
divideBtn -> setGeometry(225, 100, 75, 75);
_7Btn -> setGeometry(0, 175, 75, 75);
_8Btn -> setGeometry(75, 175, 75, 75);
_9Btn -> setGeometry(150, 175, 75, 75);
multiplyBtn -> setGeometry(225, 175, 75, 75);
_4Btn -> setGeometry(0, 250, 75, 75);
_5Btn -> setGeometry(75, 250, 75, 75);
_6Btn -> setGeometry(150, 250, 75, 75);
subtractBtn -> setGeometry(225, 250, 75, 75);
_1Btn -> setGeometry(0, 325, 75, 75);
_2Btn -> setGeometry(75, 325, 75, 75);
_3Btn -> setGeometry(150, 325, 75, 75);
addBtn -> setGeometry(225, 325, 75, 75);
_0Btn -> setGeometry(0, 400, 150, 75);
dotBtn -> setGeometry(150, 400, 75, 75);
equalsBtn -> setGeometry(225, 400, 75, 75);
connect(cancelBtn, &QPushButton::clicked, this, &MainWindow::cancel);
connect(_0Btn, &QPushButton::clicked, this, &MainWindow::_0BtnClicked);
connect(_1Btn, &QPushButton::clicked, this, &MainWindow::_1BtnClicked);
connect(_2Btn, &QPushButton::clicked, this, &MainWindow::_2BtnClicked);
connect(_3Btn, &QPushButton::clicked, this, &MainWindow::_3BtnClicked);
connect(_4Btn, &QPushButton::clicked, this, &MainWindow::_4BtnClicked);
connect(_5Btn, &QPushButton::clicked, this, &MainWindow::_5BtnClicked);
connect(_6Btn, &QPushButton::clicked, this, &MainWindow::_6BtnClicked);
connect(_7Btn, &QPushButton::clicked, this, &MainWindow::_7BtnClicked);
connect(_8Btn, &QPushButton::clicked, this, &MainWindow::_8BtnClicked);
connect(_9Btn, &QPushButton::clicked, this, &MainWindow::_9BtnClicked);
connect(dotBtn, &QPushButton::clicked, this, &MainWindow::dotBtnClicked);
connect(plusMinusBtn, &QPushButton::clicked, this, &MainWindow::plusMinusClicked);
operation = Operation::ADD;
number1 = 0;
number2 = 0;
}
void MainWindow::cancel()
{
text -> setText("");
}
void MainWindow::plusMinusClicked()
{
if((text -> text().toUtf8()).at(0) != '-')
{
text -> setText("-" + text -> text().toUtf8());
}
else
{
text -> setText(text -> text().remove(0, 1));
}
}
void MainWindow::add()
{
number2 = (text -> text()).toDouble();
if(operation == Operation::ADD)
{
number1 += number2;
}
else if(operation == Operation::SUBTRACT)
{
number1 -= number2;
}
else if(operation == Operation::MULTIPLY)
{
number1 *= number2;
}
else if(operation == Operation::DIVIDE)
{
number1 /= number2;
}
else if(operation == Operation::MODULO)
{
number1 %= number2;
}
operation = Operation::ADD;
text -> setText("");
}
void MainWindow::subtract()
{
number2 = (text -> text()).toDouble();
if(operation == Operation::ADD)
{
number1 += number2;
}
else if(operation == Operation::SUBTRACT)
{
number1 -= number2;
}
else if(operation == Operation::MULTIPLY)
{
number1 *= number2;
}
else if(operation == Operation::DIVIDE)
{
number1 /= number2;
}
else if(operation == Operation::MODULO)
{
number1 %= number2;
}
operation = Operation::SUBTRACT;
text -> setText("");
}
void MainWindow::multiply()
{
number2 = (text -> text()).toDouble();
if(operation == Operation::ADD)
{
number1 += number2;
}
else if(operation == Operation::SUBTRACT)
{
number1 -= number2;
}
else if(operation == Operation::MULTIPLY)
{
number1 *= number2;
}
else if(operation == Operation::DIVIDE)
{
number1 /= number2;
}
else if(operation == Operation::MODULO)
{
number1 %= number2;
}
operation = Operation::MULTIPLY;
text -> setText("");
}
void MainWindow::divide()
{
number2 = (text -> text()).toDouble();
if(operation == Operation::ADD)
{
number1 += number2;
}
else if(operation == Operation::SUBTRACT)
{
number1 -= number2;
}
else if(operation == Operation::MULTIPLY)
{
number1 *= number2;
}
else if(operation == Operation::DIVIDE)
{
number1 /= number2;
}
else if(operation == Operation::MODULO)
{
number1 %= number2;
}
operation = Operation::DIVIDE;
text -> setText("");
}
void MainWindow::modulo()
{
number2 = (text -> text()).toDouble();
if(operation == Operation::ADD)
{
number1 += number2;
}
else if(operation == Operation::SUBTRACT)
{
number1 -= number2;
}
else if(operation == Operation::MULTIPLY)
{
number1 *= number2;
}
else if(operation == Operation::DIVIDE)
{
number1 /= number2;
}
else if(operation == Operation::MODULO)
{
number1 %= number2;
}
operation = Operation::MODULO;
text -> setText("");
}
void MainWindow::equals()
{
text -> setText(number(number1));
}
void MainWindow::_0BtnClicked()
{
text -> setText(text -> text().toUtf8() + "0");
}
void MainWindow::_1BtnClicked()
{
text -> setText(text -> text().toUtf8() + "1");
}
void MainWindow::_2BtnClicked()
{
text -> setText(text -> text().toUtf8() + "2");
}
void MainWindow::_3BtnClicked()
{
text -> setText(text -> text().toUtf8() + "3");
}
void MainWindow::_4BtnClicked()
{
text -> setText(text -> text().toUtf8() + "4");
}
void MainWindow::_5BtnClicked()
{
text -> setText(text -> text().toUtf8() + "5");
}
void MainWindow::_6BtnClicked()
{
text -> setText(text -> text().toUtf8() + "6");
}
void MainWindow::_7BtnClicked()
{
text -> setText(text -> text().toUtf8() + "7");
}
void MainWindow::_8BtnClicked()
{
text -> setText(text -> text().toUtf8() + "8");
}
void MainWindow::_9BtnClicked()
{
text -> setText(text -> text().toUtf8() + "9");
}
void MainWindow::dotBtnClicked()
{
text -> setText(text -> text().toUtf8() + ".");
}
MainWindow::~MainWindow()
{
delete ui;
}
答案 0 :(得分:0)
如果使用高达5.4的gcc进行编译,则必须使用编译器选项-std=c++11
如果您使用的是gcc 6.1或更高版本,那么您不需要它。
使用gcc 5.4时,错误消息显示只有在使用c ++ 11选项进行编译时,范围内的枚举才可用。使用C ++ 11将Scoped枚举添加到c ++中。
<source>:2:1: warning: scoped enums only available with -std=c++11 or -std=gnu++11
enum class Operation
^
<source>: In function 'int main()':
<source>:16:14: error: 'Operation' is not a class or namespace
operation = Operation::ADD;
^
Compiler exited with result code 1