数组有效,而矢量无效。我做错了什么?
Array works while vector doesn't. What am i doing wrong?
我正在制作一个Tic-tac-toe游戏,我有一个包含9个tile类实例的board类,并用它们做一些事情。起初,我使用了一个C数组来创建这些实例Tile board[MAX_TILES]
,并且一切都很好。然而,我想使用向量,因为它们更安全,所以我对向量std::vector<Tile> board(MAX_TILES);
进行了同样的尝试,但我在.cpp文件中遇到了一堆错误,在.h文件中出现了两个错误。
我还不习惯向量,因为我们的大学教授教我使用C风格的数组。我做错了什么?
这是我的Board.h和错误,如果你需要.cpp,我会提供:
#ifndef BOARD_H
#define BOARD_H
#include <SFMLWindow.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)
{
}
只需快速查看即可,您需要在Board的构造函数中而不是在类定义中初始化向量。
C++03不支持初始值设定项列表。如果这是C++11,那么您必须使用{}。
相关文章:
- #定义c-预处理器常量..我做错了什么
- 努力将整数转换为链表。不知道我在这里做错了什么
- .h 和.cpp文件分离时出错,但仅使用 .h 文件时没有错误.我做错了什么?
- 我的C++线程做错了什么?
- 谁能告诉我我用 getline 做错了什么 (cpp) 格式
- 没有输出的合并排序我做错了什么?
- 我正在尝试使用 while 循环从字符串中删除字母,直到没有字母。我在这里做错了什么?
- 在C++中使用 AKS 素数测试计算双胞胎素数 我做错了什么?
- 我一直试图弄清楚我在这个链表程序中做错了什么
- 我正在尝试学习如何在 c++ 中传递指针,但出现错误:没有用于调用"test"的匹配函数。我做错了什么?
- FFMPEG,C++,内存泄漏,我做错了什么?
- 我做错了什么?反向字符串 C++
- 我在这个课上做错了什么?
- 创建整数的 2D 数组,该数组将使用两个函数用随机数填充矩阵.我做错了什么?
- 在不使用内置库函数的情况下添加字符串,我做错了什么?
- 我做错了什么?GetDiskFreeSpaceExA的功能根本不起作用
- 我在查找和字符串上做错了什么?
- 我正在尝试用 c++ 制作一个日志记录框架,但信息没有传递给记录器的子组件,我做错了什么?
- 我在尝试将多个值push_back向量时出错。我做错了什么?
- 插入排序:我做错了什么?