2012-08-22 56 views
0

我正在做一個井字遊戲,我有一個包含9個tile類的實例的板類,並且對它們做了些什麼。起初我用C數組來創建這些實例Tile board[MAX_TILES],並且一切正常。然而,我想使用矢量,因爲它們更安全,所以我嘗試了相同的向量std::vector<Tile> board(MAX_TILES);,但相反,我在我的.cpp文件中出現一堆錯誤,而在我的.h文件中出現了2個錯誤。陣列在矢量不工作時工作。我究竟做錯了什麼?

我不習慣使用矢量,因爲我被大學教授教過使用C風格的數組。我究竟做錯了什麼?

這是我Board.h和錯誤,如果你需要的.cpp我將提供:

#ifndef BOARD_H 
#define BOARD_H 

#include <SFML\Window.hpp> 
#include <vector> 

#include "tile.h" 

#define MAX_TILES 9 

enum Position 
{ 
    TOP_LEFT  = 0, 
    TOP_MIDDLE  = 1, 
    TOP_RIGHT  = 2, 
    MIDDLE_LEFT  = 3, 
    MIDDLE_MIDDLE = 4, 
    MIDDLE_RIGHT = 5, 
    BOTTOM_LEFT  = 6, 
    BOTTOM_MIDDLE = 7, 
    BOTTOM_RIGHT = 8 
}; 

class Board 
{ 
private: 
    std::vector<Tile> board(MAX_TILES); 
    //Tile board[MAX_TILES]; 
    void positionTiles(); 
public: 
    //Constructors 
    Board(); 
    Board(TileState tileState); 
    //Methods 
    void clearBoard(); 
    void drawBoard(sf::RenderWindow& window); 

}; 

#endif 

錯誤1和2是從h和其他來自的.cpp:

Error 1 error C2059: syntax error : 'constant' 
Error 2 error C2059: syntax error : 'constant' 
Error 3 error C2677: binary '[' : no global operator found which takes type 'Position' (or there is no acceptable conversion) 
Error 4 error C2228: left of '.setTilePosition' must have class/struct/union  
Error 5 error C2677: binary '[' : no global operator found which takes type 'Position' (or there is no acceptable conversion) 
Error 6 error C2228: left of '.setTilePosition' must have class/struct/union  
Error 7 error C2677: binary '[' : no global operator found which takes type 'Position' (or there is no acceptable conversion) 
Error 8 error C2228: left of '.setTilePosition' must have class/struct/unio 
Error 9 error C2677: binary '[' : no global operator found which takes type 'Position' (or there is no acceptable conversion) 
Error 10 error C2228: left of '.setTilePosition' must have class/struct/union  
Error 11 error C2677: binary '[' : no global operator found which takes type 'Position' (or there is no acceptable conversion) 
Error 12 error C2228: left of '.setTilePosition' must have class/struct/union  
Error 13 error C2677: binary '[' : no global operator found which takes type 'Position' (or there is no acceptable conversion) 
Error 14 error C2228: left of '.setTilePosition' must have class/struct/union  
Error 15 error C2677: binary '[' : no global operator found which takes type 'Position' (or there is no acceptable conversion) 
Error 16 error C2228: left of '.setTilePosition' must have class/struct/union  
Error 17 error C2677: binary '[' : no global operator found which takes type 'Position' (or there is no acceptable conversion) 
Error 18 error C2228: left of '.setTilePosition' must have class/struct/union  
Error 19 error C2677: binary '[' : no global operator found which takes type 'Position' (or there is no acceptable conversion) 
Error 20 error C2228: left of '.setTilePosition' must have class/struct/union  
Error 21 error C3867: 'Board::board': function call missing argument list; use '&Board::board' to create a pointer to member 
Error 22 error C2109: subscript requires array or pointer type 29 
Error 23 error C2228: left of '.setTileState' must have class/struct/union 
Error 24 error C3867: 'Board::board': function call missing argument list; use '&Board::board' to create a pointer to member 
Error 25 error C2109: subscript requires array or pointer type 
Error 26 error C2228: left of '.setTileState' must have class/struct/union 
Error 27 error C2677: binary '[' : no global operator found which takes type 'Position' (or there is no acceptable conversion) 
Error 28 error C2228: left of '.getTileSprite' must have class/struct/union 
Error 29 error C2677: binary '[' : no global operator found which takes type 'Position' (or there is no acceptable conversion)  45 
Error 30 error C2228: left of '.getTileSprite' must have class/struct/union 
Error 31 error C2677: binary '[' : no global operator found which takes type 'Position' (or there is no acceptable conversion) 
Error 32 error C2228: left of '.getTileSprite' must have class/struct/union 
Error 33 error C2677: binary '[' : no global operator found which takes type 'Position' (or there is no acceptable conversion) 
Error 34 error C2228: left of '.getTileSprite' must have class/struct/union 
Error 35 error C2677: binary '[' : no global operator found which takes type 'Position' (or there is no acceptable conversion) 
Error 36 error C2228: left of '.getTileSprite' must have class/struct/union 
Error 37 error C2677: binary '[' : no global operator found which takes type 'Position' (or there is no acceptable conversion)  49 
Error 38 error C2228: left of '.getTileSprite' must have class/struct/union 
Error 39 error C2677: binary '[' : no global operator found which takes type 'Position' (or there is no acceptable conversion) 
Error 40 error C2228: left of '.getTileSprite' must have class/struct/union 
Error 41 error C2677: binary '[' : no global operator found which takes type 'Position' (or there is no acceptable conversion) 
Error 42 error C2228: left of '.getTileSprite' must have class/struct/union 
Error 43 error C2677: binary '[' : no global operator found which takes type 'Position' (or there is no acceptable conversion) 
Error 44 error C2228: left of '.getTileSprite' must have class/struct/union 
+0

您想顯示.cpp嗎? – Vikdor

回答

3

你不能在C++ 03中做到這一點。

std::vector<Tile> board(MAX_TILES); 

你應該在c-tor中使用它。

Board():board(MAX_TILES) 
{ 
} 
0

C++ 03不支持初始值設定項列表。如果這是C++ 11,那麼你必須使用{}。

1

只需快速瀏覽一下,您需要在Board的構造函數中初始化矢量,而不是在類定義中初始化矢量。

相關問題