dan
dan

Reputation: 893

C++ Using Enum with Static Method

new to QT5 only have done a little C++. Mostly C#. anyway, trying to make an enum and use it in a static method in a class. the class file does not see the enum (compliler errors are "unknown Type name ActionNames.". tried a few things, moving enum out of the class, using Gui:: in front of ViewNames in cpp file etc. I'm sure this is a pretty easy thing - but how can I do this? thank you.

Gui.h

#ifndef GUI_H
#define GUI_H

#include <QObject>

class Gui
{
public:
    Gui();

    enum ViewNames
    {
    MAIN_VIEW,
    WORK_VIEW
    };

    enum ActionNames
    {
    BACK,
    HOME
    };
    static std::string GetViewStringFor(ViewNames view);
    static std::string GetActionStringFor(ActionNames view);
};

#endif // GUI_H

Gui.cpp

#ifndef GUI_H
#define GUI_H

#include <QObject>

#include "Gui.h"

class Gui
{
public:
    Gui();

    static std::string GetViewStringFor(ViewNames view)
    {
        return "";
    }
    static std::string GetActionStringFor(ActionNames view)
    {
        return "";
    }
};

#endif // GUI_H

Upvotes: 0

Views: 1466

Answers (2)

dan
dan

Reputation: 893

big oversight - the cpp file included class again... whoops... thanks everyone.

#include "Gui.h"

Gui::Gui()
{

}

static std::string GetViewStringFor(Gui::ViewNames view)
{
    return "";
}

static std::string GetActionStringFor(Gui::ActionNames view)
{
    return "";
}

Upvotes: 0

Jerry Jeremiah
Jerry Jeremiah

Reputation: 9618

You have declared the class twice slightly differently: once in gui.cpp and once in gui.h

The difference in the two classes is that the enum definitions aren't declared in the class in gui.cpp.

Your gui.cpp file defines the GUI_H symbol and then includes gui.h which checks for the GUI_H symbol. Because it exists nothing in gui.h is included.

So that means the class in gui.cpp is the one that is used and the one in gui.h is ignored, but the enums aren't declared in the class in gui.cpp so the compiler can't find them.

To fix it, change gui.cpp like this:

#include <QObject>
#include "Gui.h"

Gui::Gui()
{
}
static std::string Gui::GetViewStringFor(Gui::ViewNames view)
{
    return "";
}
static std::string Gui::GetActionStringFor(Gui::ActionNames view)
{
    return "";
}

Upvotes: 1

Related Questions