为什么我的编译器抱怨这不是类或命名空间?

时间:2015-09-20 23:54:42

标签: c++ qt header-files

我正试图在QT中编写一个扫雷游戏,但我每走一步都会感冒。目前,QT Creator正在抱怨以下代码:

> #include <QApplication>
  #include "mainwindow.h"
  #include "sweepermodel.h"
  #include <iostream>
  #include <QTime>
  #include <string>

  int main(int argc, char *argv[])
  {
      QApplication a(argc, argv);
      SweeperModel *sweeperModel = new SweeperModel(16, 16, 40);
      sweeperModel->gameState = SweeperModel::GAME_STATE::Playing;
      MainWindow w;
      w.show();

      return a.exec();
  }

它声明:

  

“C:\ Users \ nthexwn \ Workspace \ AISweeper \ main.cpp:12:错误:'SweeperModel :: GAME_STATE'不是类或命名空间”

回到SweeperModel头文件,我们可以看到GAME_STATE确实是在那里声明的枚举:

#ifndef SWEEPERMODEL
#define SWEEPERMODEL

#include <vector>
#include "sweepernode.h"

// Abstraction of the game grid as a 1-dimensional vector along with a flag
// indicating game state.
class SweeperModel
{
public:

// Possible game states from a player's perspective.
enum GAME_STATE
{
    Loading,
    Error_Height,
    Error_Width,
    Error_Mines,
    Playing,
    Lost,
    Won,
    Exiting,
};

GAME_STATE gameState;
short height;
short width;
short mines;

int getRandomValue(int low, int high);
void assignMinesToModel(SweeperModel *sweeperModel);
SweeperModel(short height, short width, short mines);
~SweeperModel();
SweeperNode& getNode(short row, short column);

private:
    std::vector<SweeperNode*> nodes;
};

#endif // SWEEPERMODEL

我忘记了什么?我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:1)

首先,enum没有创建命名空间,所以你的代码应该是

sweeperModel->gameState = SweeperModel::Playing;

第二,c ++ 11推荐枚举类,如

enum class enum_name{ firstone, secondone, thirdone};

如果您添加关键字&#34; class&#34;在&#34; enum&#34;后面,它也运作良好。 最后,MSVC会自动将枚举视为名称空间,因此您的代码也可以在MSVC中正常运行;