枚举成员不是类型错误

问题描述:

我正在C++中制作一个Rubik的2x2x2多维数据集模拟器控制台应用程序(我使用的IDE是Code :: Blocks)。现在,我试图实现所有6个面对转(L去左等)和立方体本身。问题是,我发现了一个意外的错误,说:枚举成员不是类型错误

Error: White does not name a type

Error: Red does not name a type

...and so on

这里是我的代码:

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

我想这个声明在主要和它运行平稳:

Face f(White); 

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

+0

尝试'静态面F1,F2,F3, f4,f5,f6;'然后在'Cube'的构造函数中:'f1 = Face(White); etc ..' – DimChtz

+0

请把你的问题削减到[mcve]。所有的魔方都会阻碍实际的问题。 – AndyG

这条线是你的问题:

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您使用的是你需要

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); 

定义它们。如果你是好奇什么做,那么请记住:

int foo(int); 

是函数的声明。在你的类(简体):

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

这是试图声明(没有提供实现),拥有3个静态功能类,F1F2F3,全部返回一张脸。 f1需要White类型的参数,f2需要Red类型的参数,并且f3需要蓝色。但白色,红色蓝色不是类型,它们是普查员。这就是你得到这个错误的原因。这是“C++最令人头疼的解析”的另一种变体。

然而,使其工作,你可以使用一个初始化,而不是括号声明变量,你必须声明静态成员常量

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