访问指向矢量的矢量中的数据

Access data in a pointed-to vector of vectors

本文关键字:数据 访问      更新时间:2023-10-16

是的,我知道这可能看起来是一个奇怪而复杂的标题,但这里有一个场景,我有一个名为地形的类,它存储并在瓷砖的正方形地形上执行操作,瓷砖是类,地形存储在二维数组中(作为网格(,我有个成员,他的目的是将瓷砖分配给坐标,坐标是向量中的一个单元,但是当字符串执行此操作时,编译器会给我一个长错误,其中运算符=:

||=== 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矢量的指针。在大多数情况下,矢量不需要指向。