C++宏对象创建
我认为我对宏的理解是错误的。我正在使用诅咒来制作一个游戏,我想立即生成一张地图,因为我找不到任何一个分布采样器可以工作C++宏对象创建,c++,object,macros,C++,Object,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::stri
#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]);
}
}
}
编辑-
这是我的瓷砖课:
#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-工作区图片
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);
}
应该是:
#define WATER Tile(COLOR_BLUE,COLOR_CYAN,std::string(1, static_cast<char>(247)))
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);
}
应该是:
#define WATER Tile(COLOR_BLUE,COLOR_CYAN,std::string(1, static_cast<char>(247)))
我同意上面的观点。往后推才是出路。但这里是宏方法,用赋值运算符加载向量。干杯:
#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;
}
我同意上面的观点。往后推才是出路。但这里是宏方法,用赋值运算符加载向量。干杯:
#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;
}
你确定你在包含和定义瓷砖吗?是的,我在我的地图中包含了所有内容。除了一个函数,你可以使用C++11中的初始值设定项列表:std::vector tiles{WATER,LAVA,ANIMAL,PATH};是的,我就是这么做的。我不得不在eclipse中添加这个标志,我想知道为什么它以前不起作用,然后我在其他地方读到我需要这个标志。你确定你包含了并定义了瓷砖吗?是的,我包含了我地图中的所有内容。除了一个函数,你可以在C++11:std::vector瓷砖{WATER,LAVA,ANIMAL,PATH}中使用初始值设定项列表;是的,我就是这么做的。我不得不在eclipse中添加这个标志,我想知道为什么它以前不起作用,然后我在其他地方读到我需要这个标志。你忘了包括一些标题?例如:包括问题,或用新错误更新问题,请更新它。抱歉耽搁了。哦,是的,我之前有过std::string。我把它改成了char。但我又把它改了回去,按照你的建议做了。我将再次更新我的问题,以稍微反映这些变化。在同一条线上,我仍然有一些错误,但我想我最终能找出它们。非常感谢你的帮助。事实上,我刚刚用我工作区的截图再次更新了它。你能再帮我一次吗?我不知道是怎么回事。这是C++11,初始化列表,VS可能不支持它。您仍然必须使用push_back,但它将在GCC最新版本中工作。您忘记包含一些标题了吗?例如:包括问题,或用新错误更新问题,请更新它。抱歉耽搁了。哦,是的,我之前有过std::string。我把它改成了char。但我又把它改了回去,按照你的建议做了。我将再次更新我的问题,以稍微反映这些变化。在同一条线上,我仍然有一些错误,但我想我最终能找出它们。非常感谢你的帮助。事实上,我刚刚用我工作区的截图再次更新了它。你能再帮我一次吗?我不知道是怎么回事。这是C++11,初始化列表,VS可能不支持它。您仍然需要使用push_back,但它将在GCC最新版本中工作。