2017-07-29 182 views
-1

我正在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

回答

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

0

定義它們。如果你是好奇什麼做,那麼請記住:

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