1

我认为我对宏的理解是错误的。我正在使用 curses 制作游戏,我想生成一张地图(现在统一挑选瓷砖,因为我无法让任何我发现工作的分布采样器)。

#include "Map.h"

/*  Our ingame tiles    */
#define WATER   Tile(COLOR_BLUE,COLOR_CYAN,std::string(1, static_cast<char>(247)));
#define LAVA    Tile(COLOR_YELLOW, COLOR_RED, std::string(1, static_cast<char>(247)));
#define ANIMAL  Tile(COLOR_MAGENTA, COLOR_BLACK, std::string(1, static_cast<char>(224)));
#define PATH    Tile(COLOR_WHITE, COLOR_WHITE, std::string(1, static_cast<char>(32)));

//static int pathPx = 5;
//static int lavaPx = 2;
//static int waterPx = 2;
//static int animalPx = 1;

std::vector<Tile> tiles = {PATH, LAVA, WATER, ANIMAL};

std::vector< std::vector<Tile> > map;


Map::Map(){
    srand(time(NULL));
    int y = rand() % 10 + 10; 
    int x = rand() % 10 + 10;
    for (int j=0; j<y; j++){
        std::vector<Tile> tileRowi;
        for (int i=0; i<x; i++){
            int n = rand()%4+1;
            tileRowi.push_back(tiles[n]);
        }
    }
}

错误是:

   First: c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/basic_string.h   initializing argument 1 of 'std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]' [-fpermissive] 

第二个:invalid conversion from 'char' to 'const char*' [-fpermissive]

第一个说我的一个文件的第 485 行......但没有第 485 行。第二个在:

    std::vector<Tile> tiles = {PATH, LAVA, WATER, ANIMAL};

编辑 -

这是我的瓷砖课程:

 #include "Tile.h"


int backgroundColor;
int foregroundColor;
std::string rep;

Tile::Tile(int fc, int bc, std::string r){
    backgroundColor = bc;
    foregroundColor = fc;
    rep = r;
}

编辑 2--工作区图片 这是错误

4

2 回答 2

4

C++ 不允许在函数外进行操作,请将下面的代码放入函数中

tiles.push_back(WATER);
tiles.push_back(LAVA);
tiles.push_back(ANIMAL);
tiles.push_back(PATH);

地图.cpp:

void MakeTiles(std::vector<Tile>& tiles)
{
  tiles.push_back(WATER);
  tiles.push_back(LAVA);
  tiles.push_back(ANIMAL);
  tiles.push_back(PATH);
}

编辑

由于您尚未发布 Tile 课程,因此我不得不猜测您最后发生了什么。看起来您在 Tile 中有一个 std::string 类型成员,它需要一个 char 来构造它(请参阅 Tile 的第三个参数)?一个可能的解决方法是:

  std::string(1, static_cast<char>(247));

最后你的宏还有额外;

#define WATER   Tile(COLOR_BLUE,COLOR_CYAN,std::string(1, static_cast<char>(247)));
                                                                                  ^^^^

应该:

#define WATER   Tile(COLOR_BLUE,COLOR_CYAN,std::string(1, static_cast<char>(247)))
于 2012-12-23T04:30:09.927 回答
0

我同意上面的帖子。push_back 是要走的路。但这是宏方式,使用赋值运算符加载向量。干杯:)

#include <iostream>
#include <vector>

#define WHITE tiles white(1,'w') //messy ihmo
#define BLACK tiles black(-1,'b')

using namespace std;

class tiles
{
public:
tiles(int NUMBER, char LETTER):number(NUMBER), letter(LETTER){};
int number;
char letter;
};

int main()
{
WHITE; //macro overwrites here
BLACK;

vector<tiles> box; //initialize the vector
box.reserve(8); //must do this manually if you don't use stl push_back()
                //or get memory error, bounds issue.  push_back() is really the better way 
                //to go.
int i=0;
box[i++]=white;//load up the vector via the assignment operator 
box[i]=black;//same

cout<<"WHITE "<<box[0].letter<<" BLACK "<<box[1].letter<<endl;//check to screen
cout<<"WHITE "<<box[0].number<<" BLACK "<<box[1].number<<endl;

system("pause");
return 0;
}
于 2012-12-23T07:37:59.257 回答