Search code examples
c++enumsconsoleconsole-applicationrubiks-cube

Enum member not a type error


I'm making a Rubik's 2x2x2 cube simulator console app in C++ (the IDE I'm using is Code::Blocks). For now, I'm trying to implement all 6 face turns (L goes for left etc.) and the cube itself. Problem is, I'm getting an unexpected error that says:

Error: White does not name a type

Error: Red does not name a type

...and so on

Here's my code:

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

I tried this declaration in main and it runs smoothly:

Face f(White);

Any ideas on why I'm getting this and how to solve it?


Solution

  • This line is your problem:

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

    Basically it doesn't like the way you are initializing f1 et al.

    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.

    Then just as a general observation: IMO you are using static more than you need to