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

时间:2014-05-21 20:31:13

标签: c++ vector

是的我知道这似乎是一个奇怪而复杂的标题但是这里有一个坐标,我有一个名为terrain的类,它存储并在Tiles的方形地形上进行动作,这些是类,地形存储在两个dimensionnal数组(用于网格),我有一个成员,其目的是将一个Tile分配给一个Coordinate,这是一个向量中的一个单元格,但是当这样做时,编译器给了我一个很长的错误,运算符=:

||=== Build: Debug in TilesWar (compiler: GNU GCC Compiler) ===|
C:\Users\qbsec_000\Traxys\Documents\Cpp\Programms\TilesWar\terrain.cpp||In member function 'void Terrain::setTileFromCoord(Coord, Tile)':|
C:\Users\qbsec_000\Traxys\Documents\Cpp\Programms\TilesWar\terrain.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:\Users\qbsec_000\Traxys\Documents\Cpp\Programms\TilesWar\terrain.cpp|15|note: candidates are:|
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\vector.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:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\vector.tcc|160|note:   no known conversion for argument 1 from 'Tile' to 'const std::vector<Tile>&'|
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_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:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_vector.h|439|note:   no known conversion for argument 1 from 'Tile' to 'std::vector<Tile>&&'|
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_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:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_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,肯定会被更改

感谢The One我得到了我的答案,他正在重复指针,谢谢!

1 个答案:

答案 0 :(得分:0)

您的问题是您尝试将Tile分配到指向矢量的指针,而不是矢量本身。您可以将Terrain::setTileFromCoord中的行更改为terrain->operator[](5)[10] =tile;,或者如您的代码所示,从std::vector< std::vector <Tile> > *terrain;的定义中删除aestrik(*),因为这会使您的变量成为指针矢量瓷砖矢量。在大多数情况下,不需要指向矢量。