繁体   English   中英

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

[英]Why is my compiler complaining that this isn't a class or namespace?

我正在尝试用QT编写扫雷游戏,但我在每一步都感到沮丧。 目前,QT创建者正在抱怨以下代码:

> #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

我在这里忘记了什么? 我该如何进行这项工作?

首先,枚举不会创建名称空间,因此您的代码应为

sweeperModel->gameState = SweeperModel::Playing;

其次,c ++ 11要求枚举类,例如

enum class enum_name{ firstone, secondone, thirdone};

如果在“枚举”后面添加关键字“类”,则效果也很好。 最后,MSVC会自动将枚举视为拥有枚举名称空间,因此您的代码也将在MSVC中正常工作;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM