作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在制作一个井字游戏,我有一个棋盘类,其中包含 9 个图 block 类实例并用它们做一些事情。起初我使用 C 数组来创建这些实例 Tile board[MAX_TILES]
一切都对他们很好。但是我想改用 vector ,因为它们更安全,所以我尝试了同样的 vector std::vector<Tile> board(MAX_TILES);
但是我的 .cpp 文件中出现了一堆错误,.h 文件中出现了 2 个错误。
我还不习惯 vector ,因为我们的大学教授教我使用 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
最佳答案
你不能在 C++03 中这样做。
std::vector<Tile> board(MAX_TILES);
你应该在 c-tor 中使用它。
Board():board(MAX_TILES)
{
}
关于c++ - 数组有效,而 vector 无效。我究竟做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12065796/
我是一名优秀的程序员,十分优秀!