访问指向矢量的矢量中的数据
Access data in a pointed-to vector of vectors
是的,我知道这可能看起来是一个奇怪而复杂的标题,但这里有一个场景,我有一个名为地形的类,它存储并在瓷砖的正方形地形上执行操作,瓷砖是类,地形存储在二维数组中(作为网格(,我有个成员,他的目的是将瓷砖分配给坐标,坐标是向量中的一个单元,但是当字符串执行此操作时,编译器会给我一个长错误,其中运算符=:
||=== Build: Debug in TilesWar (compiler: GNU GCC Compiler) ===|
C:Usersqbsec_000TraxysDocumentsCppProgrammsTilesWarterrain.cpp||In member function 'void Terrain::setTileFromCoord(Coord, Tile)':|
C:Usersqbsec_000TraxysDocumentsCppProgrammsTilesWarterrain.cpp|15|error: no match for 'operator=' (operand types are '__gnu_cxx::__alloc_traits<std::allocator<std::vector<Tile> > >::value_type {aka std::vector<Tile>}' and 'Tile')|
C:Usersqbsec_000TraxysDocumentsCppProgrammsTilesWarterrain.cpp|15|note: candidates are:|
c:mingwlibgccmingw324.8.1includec++bitsvector.tcc|160|note: std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(const std::vector<_Tp, _Alloc>&) [with _Tp = Tile; _Alloc = std::allocator<Tile>]|
c:mingwlibgccmingw324.8.1includec++bitsvector.tcc|160|note: no known conversion for argument 1 from 'Tile' to 'const std::vector<Tile>&'|
c:mingwlibgccmingw324.8.1includec++bitsstl_vector.h|439|note: std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(std::vector<_Tp, _Alloc>&&) [with _Tp = Tile; _Alloc = std::allocator<Tile>]|
c:mingwlibgccmingw324.8.1includec++bitsstl_vector.h|439|note: no known conversion for argument 1 from 'Tile' to 'std::vector<Tile>&&'|
c:mingwlibgccmingw324.8.1includec++bitsstl_vector.h|461|note: std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(std::initializer_list<_Tp>) [with _Tp = Tile; _Alloc = std::allocator<Tile>]|
c:mingwlibgccmingw324.8.1includec++bitsstl_vector.h|461|note: no known conversion for argument 1 from 'Tile' to 'std::initializer_list<Tile>'|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|
以下是一些与之相关的代码:
#ifndef TERRAINN_H_INCLUDED
#define TERRAINN_H_INCLUDED
#include <vector>
#include "Tile.h"
typedef struct Coord Coord;
struct Coord{
int x;
int y;
};
class Terrain{
public:
Terrain();
Terrain(int tSize);
Terrain(int terrainSize,/*std::string terainTheme,*/int x1,int y1,int x2,int y2,int x3,int y3,int x4,int y4); //TODO: Compress the coordonates
bool isTileClear(int x,int y); //TODo: COmpress the coord
bool getTexture(int x, int y);//TODO: Compress the coord
void setTileFromCoord(Coord point, Tile tile);
void setSpawnPoint(Coord point);
private:
std::vector< std::vector <Tile> > *terrain;
//std::string terrainTheme; //Unused with the fact that texture are not designated by the terrain but by the XML file
int terrainSize; //This is mentionned in the [Map].xml file
};
#endif // TERRAINN_H_INCLUDED
#ifndef TILE_H_INCLUDED
#define TILE_H_INCLUDED
#include <string>
class TileModel{
public:
TileModel();
TileModel(bool tileType, std::string texturePath); //Created by using XMLHandling
bool getTileType(); //Return the clear(false) or notClear(true)
std::string getTexturePath();
private:
bool tileType;
std::string texturePath;
};
class Tile{
public:
Tile();
Tile(TileModel* model);
TileModel getModel();
private:
TileModel* model;
};
#endif // TILE_H_INCLUDED
Terrain::Terrain(int tSize){
terrain = new vector<vector<Tile>>(tSize,vector<Tile>(tSize));
}
void Terrain::setTileFromCoord(Coord point,Tile tile){
terrain[5][10] = tile;
}
Tile::Tile(TileModel *model) : model(model){
}
Tile::Tile() : model(0){
model = new TileModel(false,"Data/Texture/debugPath.png");
}
我无法理解更多的错误(我刚刚开始混淆STL和向量(,所以有人能向我解释错误吗,也许可以给我一个修复或提示,谢谢!
PS有些代码是WIP,肯定会更改
感谢那个我有答案的人,他正在重新邀请指针,谢谢!
您的问题是试图将Tile分配给一个指向向量的指针,而不是向量本身。您可以将Terrain::setTileFromCoord
中的行更改为terrain->operator[](5)[10] =tile;
,或者根据您的代码,从std::vector< std::vector <Tile> > *terrain;
的定义中删除aestrik(*(,因为这使您的变量成为指向Tiles矢量的指针。在大多数情况下,矢量不需要指向。
相关文章:
- 用于访问容器<T>数据成员的正确 API
- 使用指针访问数组中的对象数据成员
- 友元函数无法访问私有数据成员 (c++)
- OpenCV C++ 3 维垫数据访问错误值
- 用于随机数据访问的最有效文件类型
- 用于多维数据访问的重载 () 运算符
- MEX C++原始数据访问
- C++数据访问基准
- 随机 mmaped 内存访问比堆数据访问慢 16%
- 共享内存多线程和数据访问
- 如何使用常见的C 业务逻辑和数据访问层构建跨平台移动应用程序
- 链表数据访问
- OpenMP线程、数据访问延迟和STL数据容器
- C++ 矢量数据访问
- 我很难在C++类中实现我想要的数据访问
- 概括模型中的数据访问.避免宏观的方法
- 矢量化/优化循环,用于宽寄存器(特别是Xeon Phi)的未对齐数据访问
- 松类耦合和数据访问
- 在Linux上锁定对单个进程内数据访问的最快方法
- 如何使用数据访问对象进行序列化和关系数据库数据访问