-2

I get "'Operation' is not a class or namespace" (mainwindow.cpp file)in every line I use Operation::ADD, Operation::SUBTRACT, Operation::MULTIPLY or 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;
}
3
  • Change enum class Operation to enum Operation. Commented Jul 19, 2017 at 20:30
  • 7
    @CeruleanChelonii That is a bad suggestion.
    – Justin
    Commented Jul 19, 2017 at 20:31
  • 1
    Are you compiling with C++11? enum class was added in C++11. Use -std=c++11 as compiler option if you are using g++ or clang. Commented Jul 19, 2017 at 20:35

1 Answer 1

0

If you are compiling with gcc up to 5.4, then you have to use compiler option -std=c++11
If you are using gcc 6.1 or higher, then you don't need that.

With gcc 5.4 the error message says that scoped enums are available only when compiling with c++11 option. Scoped enums was added to c++ with C++11.

<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
4
  • Thank you so much for a quick response! Commented Jul 19, 2017 at 22:04
  • I got a warning "scoped enums only available with -std=c++11 or -std=g++11 [enabled by default]" Commented Jul 19, 2017 at 22:05
  • I assume "[enabled by default]" means that i am already using -std=c++11 flag. Sorry about three comments instead of one but i did not realize that pressing enter posts the message instead of adding new line. Commented Jul 19, 2017 at 22:07
  • According to this post "enabled by default" comes from QT-Creator and means something else. Commented Jul 20, 2017 at 7:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.