-1

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?

Community
  • 1
  • 1
Cosmin Petolea
  • 119
  • 1
  • 10
  • Try `static Face f1, f2, f3, f4, f5, f6;` and then inside `Cube`'s constructor: `f1 = Face(White); etc..` – DimChtz Jul 29 '17 at 21:28
  • Please pare your question down to a [mcve]. All the Rubik's cube stuff just gets in the way of the actual problem. – AndyG Jul 29 '17 at 21:36

3 Answers3

2

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

0
Face f(White);

declares and defines an instance f of type Face and constructs it with argument White.

But

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

within the class definition tries to declare six static element-functions, returning an instance of class Face. A function declaration needs parameter types in the parenthesis, but you pass in enum elements White, Red, etc. which the compiler complains about.

You probably want to have six instances of class Face constructed with the given colors.

To achieve that, replace the line by

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

and outside the class define them with

Face Cube::f1(White);

etc.

bjhend
  • 1,538
  • 11
  • 25
0

If you are curious what it is doing, then remember that:

int foo(int);

Is a declaration for a function. In your class (simplified):

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

This is attempting to declare (without providing an implementation) a class that has 3 static functions, f1, f2, and f3, all returning a Face. f1 takes an argument of type White, f2 takes an argument of type Red, and f3 takes a Blue. But White, Red, and Blue are not types, they are enumerators. That's why you're getting this error. This is another variation of "C++'s most vexing parse".

However, to make it work, you can use an initializer rather than parenthesis to declare variables, and you must declare the static members const:

enum Face { White, Red, Blue };
class X {
    static const Face f1{White}, f2{Red}, f3{Blue};
};
Chris Uzdavinis
  • 6,022
  • 9
  • 16