2016-04-02 37 views
-1

在我的主函數中使用類時出現兩個錯誤。在主函數中使用類時出現兩個錯誤C++

第一誤差 -

error C2227: left of '->digitarray' must point to class/struct/union/generic type 

第二誤差是 -

error C2675: unary '~' : 'game' does not define this operator or a conversion to a type acceptable to the predefined operator 

頭文件 -

class game{ 
private: 
    int cows(); 
    int bulls(); 
    bool game_over = false; 

public: 
    int x; 
    number *user, *computer; 
    game(); 
    ~game(); 
    game(const number,const number); 
    void play(); 
}; 

主文件 -

int main(){ 
    game(); 
    for (int i = 0; i < SIZE; i++){ 
     cout << game::computer->digitarray[i].value; 
    } 

    ~game(); 

} 

與「數量」頭文件 -

#define SIZE 4 

class number{ 
private: 
public: 
    digit digitarray[SIZE]; 
    number(); 
    void numscan(); 
    void randomnum(); 
    int numreturn(int); 
    void numprint(); 
}; 
+3

__No,你不叫自己的析構函數__ – ForceBru

+0

並具有獨立的構造函數調用是錯也!。只需聲明一個「遊戲」類型的變量。 –

+0

計算機變量是一個非靜態成員變量。這些變量只能使用對象訪問。不應該明確調用析構函數。 – Pratap

回答

2

修復的方法是非常簡單的,聲明一個變量game類型:

int main(){ 
    game g; 
     // ^^ 
    for (int i = 0; i < SIZE; i++){ 
     cout << g.computer->digitarray[i].value; 
      // ^^ 
    } 

    // ~game(); <<< You don't need this or g.~game(); 
} // <<< That's done automatically here 
0

下面的東西是你的代碼錯誤。

1)您還沒有創建反對並嘗試訪問類成員,這隻能爲靜態類成員完成。

2)你不能明確調用析構函數。

+0

2) - 其實你可以:) –

相關問題