-1

我正在用 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);

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

4

3 回答 3

2

这条线是你的问题:

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

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

根据这篇文章,静态变量应该在类之外定义。

相关:为什么它没有意义以及为什么不能在构造函数中初始化静态成员。

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

于 2017-07-29T21:38:18.643 回答
0
Face f(White);

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

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

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

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

为此,将行替换为

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

在类之外定义它们

Face Cube::f1(White);

等等

于 2017-07-29T21:44:51.613 回答
0

如果您好奇它做什么,请记住:

int foo(int);

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

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

这是试图声明(不提供实现)一个具有 3 个静态函数f1f2f3的类,它们都返回一个 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};
};
于 2017-07-29T22:10:39.343 回答