C ++宏创建对象

时间:2012-12-23 04:26:29

标签: c++ object macros

我认为我对Macros的理解是不对的。我正在使用诅咒来制作一个游戏而且我想生成一张地图(现在统一采摘瓷砖,因为我找不到任何我发现的分配采样器)。

#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};

修改 -

这是我的Tile课程:

 #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--工作区图片 Here are the errors

2 个答案:

答案 0 :(得分:4)

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

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

Map.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)))

答案 1 :(得分: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;
}