枚举成员不是类型错误

Enum member not a type error

本文关键字:错误 类型 枚举成员      更新时间:2023-10-16

我正在C++中制作一个魔方的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(White), f2(Red), f3(Blue), f4(Yellow), f5(Orange), f6(Green);

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

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

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

然后作为一般观察: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,都返回一个 Face。f1 采用白色类型的参数,f2采用红色类型的参数,f3采用蓝色。白色红色蓝色不是类型,它们是枚举器。 这就是您收到此错误的原因。 这是"C++最令人烦恼的解析"的另一种变体。

但是,要使其正常工作,可以使用初始值设定项而不是括号来声明变量,并且必须声明静态成员const

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