【问题标题】:Enum member not a type error [duplicate]枚举成员不是类型错误[重复]
【发布时间】:2017-07-29 21:02:35
【问题描述】:

我正在用 C++ 制作一个 Rubik 的 2x2x2 立方体模拟器控制台应用程序(我使用的 IDE 是 Code::Blocks)。现在,我正在尝试实现所有 6 个面转弯(L 代表左等)和立方体本身。问题是,我收到了一个意外错误:

错误:白色没有命名类型

错误:红色没有命名类型

...等等

这是我的代码:

/// THIS PROGRAM SHOULD OUTPUT THE FASTEST SOLUTION TO A 2x2 RUBIK'S CUBE
#include <iostream>
#include <vector>
using namespace std;

/**
AVOID:
    - SAME MOVE THREE TIMES
    - REVERSE MOVE AFTER MOVE
*/

template<class T>
bool Check(vector<T> arr)
{
    const int a0 = arr[0];

    for (int i = 1; i < arr.size(); i++)
    {
        if (arr[i] != a0)
            return false;
    }
    return true;
}

enum Color {White, Red, Blue, Yellow, Orange, Green};

class Face
{
public:
    Face(Color cl)
    {
        c.resize(4, cl);
    }
    vector<Color> c;
};

class Cube
{
public:
    inline static void Turn(char c, bool reversed)
    {
        switch(c)
        {
            case 'L': L(reversed); break;
            case 'R': R(reversed); break;
            case 'U': U(reversed); break;
            case 'D': D(reversed); break;
            case 'F': F(reversed); break;
                case 'B': B(reversed); break;
            default: cout<<"ERROR: "<<c<<" is not a valid rubik's cube turn         notation.\n";
        }
    }

    bool Solved(){return true;}

private:
static Color aux[2];
static Face f1(White), f2(Red), f3(Blue), f4(Yellow), f5(Orange), f6(Green);
static void L(bool reversed) //f1, f2, f4, f5
{
    if(reversed)
    {
        aux[0]=f1.c[0];
        aux[1]=f1.c[2];

        f1.c[0]=f2.c[0];
        f1.c[2]=f2.c[2];

        f2.c[0]=f4.c[0];
        f2.c[2]=f4.c[2];

        f4.c[0]=f5.c[0];
        f4.c[2]=f5.c[2];

        f5.c[0]=aux[0];
        f5.c[2]=aux[1];

        return;
    }

    aux[0]=f5.c[0];
    aux[1]=f5.c[2];

    f5.c[0]=f4.c[0];
    f5.c[2]=f4.c[2];

    f4.c[0]=f2.c[0];
    f4.c[2]=f2.c[2];

    f2.c[0]=f1.c[0];
    f2.c[2]=f1.c[2];

    f1.c[0]=aux[0];
    f1.c[2]=aux[1];
}

static void R(bool reversed)
{
    if(!reversed)
    {
        aux[0]=f1.c[1];
        aux[1]=f1.c[3];

        f1.c[1]=f2.c[1];
        f1.c[3]=f2.c[3];

        f2.c[1]=f4.c[1];
        f2.c[3]=f4.c[3];

        f4.c[1]=f5.c[1];
        f4.c[3]=f5.c[3];

        f5.c[1]=aux[0];
        f5.c[3]=aux[1];

        return;
    }

    aux[0]=f1.c[1];
    aux[1]=f1.c[3];

    f1.c[1]=f2.c[1];
    f1.c[3]=f2.c[3];

    f2.c[1]=f4.c[1];
    f2.c[3]=f4.c[3];

    f4.c[1]=f5.c[1];
    f4.c[3]=f5.c[3];

    f5.c[1]=aux[0];
    f5.c[3]=aux[1];
}

static void U(bool reversed) //f2, f3, f5, f6
{
    if(!reversed)
    {
        aux[0]=f2.c[0];
        aux[1]=f2.c[1];

        f2.c[0]=f3.c[0];
        f2.c[1]=f3.c[1];

        f3.c[0]=f5.c[0];
        f3.c[1]=f5.c[1];

        f5.c[0]=f6.c[0];
        f5.c[1]=f6.c[1];

        f6.c[0]=aux[0];
        f6.c[1]=aux[1];

        return;
    }

    aux[0]=f6.c[0];
    aux[1]=f6.c[1];

    f6.c[0]=f5.c[0];
    f6.c[1]=f5.c[1];

    f5.c[0]=f3.c[0];
    f5.c[1]=f3.c[1];

    f3.c[0]=f2.c[0];
    f3.c[1]=f2.c[1];

    f2.c[0]=aux[0];
    f2.c[1]=aux[1];
}

static void D(bool reversed)
{
    if(reversed)
    {
        aux[0]=f2.c[2];
        aux[1]=f2.c[3];

        f2.c[2]=f3.c[2];
        f2.c[3]=f3.c[3];

        f3.c[2]=f5.c[2];
        f3.c[3]=f5.c[3];

        f5.c[2]=f6.c[2];
        f5.c[3]=f6.c[3];

        f6.c[2]=aux[0];
        f6.c[3]=aux[1];

        return;
    }

    aux[0]=f6.c[2];
    aux[1]=f6.c[3];

    f6.c[2]=f5.c[2];
    f6.c[3]=f5.c[3];

    f5.c[2]=f3.c[2];
    f5.c[3]=f3.c[3];

    f3.c[2]=f2.c[2];
    f3.c[3]=f2.c[3];

    f2.c[2]=aux[0];
    f2.c[3]=aux[1];
}

static void F(bool reversed) //f1, f3, f4, f6
{
    if(reversed)
    {
        aux[0]=f1.c[2];
        aux[1]=f1.c[3];

        f1.c[2]=f3.c[2];
        f1.c[3]=f3.c[3];

        f3.c[2]=f4.c[2];
        f3.c[3]=f4.c[3];

        f4.c[2]=f6.c[2];
        f4.c[3]=f6.c[3];

        f6.c[2]=aux[0];
        f6.c[3]=aux[1];

        return;
    }

    aux[0]=f6.c[2];
    aux[1]=f6.c[3];

    f6.c[2]=f4.c[2];
    f6.c[3]=f4.c[3];

    f4.c[2]=f3.c[2];
    f4.c[3]=f3.c[3];

    f3.c[2]=f1.c[2];
    f3.c[3]=f1.c[3];

    f1.c[2]=aux[0];
    f1.c[3]=aux[1];
}
static void B(bool reversed)
{
    if(!reversed)
    {
        aux[0]=f1.c[0];
        aux[1]=f1.c[1];

        f1.c[0]=f3.c[0];
        f1.c[1]=f3.c[1];

        f3.c[0]=f4.c[0];
        f3.c[1]=f4.c[1];

        f4.c[0]=f6.c[0];
        f4.c[1]=f6.c[1];

        f6.c[0]=aux[0];
        f6.c[1]=aux[1];

        return;
    }

    aux[0]=f6.c[0];
    aux[1]=f6.c[1];

    f6.c[0]=f4.c[0];
    f6.c[1]=f4.c[1];

    f4.c[0]=f3.c[0];
    f4.c[1]=f3.c[1];

    f3.c[0]=f1.c[0];
    f3.c[1]=f1.c[1];

    f1.c[0]=aux[0];
    f1.c[1]=aux[1];
}
};

int main()
{

    return 0;
}    
/// THIS PROGRAM SHOULD OUTPUT THE FASTEST SOLUTION TO A 2x2 RUBIK'S CUBE

我在 main 中尝试了这个声明,它运行顺利:

Face f(White);

关于我为什么会得到这个以及如何解决它的任何想法?

【问题讨论】:

  • 尝试static Face f1, f2, f3, f4, f5, f6;,然后在Cube的构造函数中:f1 = Face(White); etc..
  • 请将您的问题简化为minimal reproducible example。所有魔方的东西都会妨碍实际问题。

标签: c++ enums console console-application rubiks-cube


【解决方案1】:

这一行是你的问题:

static Face f1(White), f2(Red), f3(Blue), f4(Yellow), f5(Orange), f6(Green);

基本上它不喜欢你初始化 f1 等的方式。

Static variables should be defined outside of the class according to this post.

Related: Why it doesn't make sense and why you can't initialize static members in the constructor.

然后作为一般观察:IMO 您使用静态的次数超出了您的需要

【讨论】:

    【解决方案2】:
    Face f(White);
    

    声明并定义Face类型的实例f并使用参数White构造它。

    但是

    static Face f1(White), f2(Red), f3(Blue), f4(Yellow), f5(Orange), f6(Green);
    

    在类定义中尝试声明六个静态元素函数,返回类Face 的一个实例。函数声明需要括号中的参数类型,但您传入了编译器抱怨的枚举元素WhiteRed 等。

    您可能希望使用给定的颜色构造六个 Face 类实例。

    为此,将行替换为

    static Face f1, f2, f3, f4, f5, f6;
    

    在类之外定义它们

    Face Cube::f1(White);
    

    等等

    【讨论】:

      【解决方案3】:

      如果你好奇它在做什么,那么请记住:

      int foo(int);
      

      是一个函数的声明。在你的课堂上(简体):

      enum Face { White, Red, Blue };
      class X {
          static Face f1(White), f2(Red), f3(Blue);
      };
      

      这是试图声明(不提供实现)一个具有 3 个静态 函数f1f2 的类>f3,都返回一个Face。 f1 采用 White 类型的参数,f2 采用 Red 类型的参数,而 f3 采用 Blue 类型。但是 WhiteRedBlue 不是类型,它们是枚举器。这就是您收到此错误的原因。这是“C++ 最令人头疼的解析”的另一种变体。

      但是,要使其工作,您可以使用初始化程序而不是括号来声明变量,并且您必须声明静态成员const

      enum Face { White, Red, Blue };
      class X {
          static const Face f1{White}, f2{Red}, f3{Blue};
      };
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-12
        • 2022-01-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-08
        相关资源
        最近更新 更多