Hola. Tengo un programa hecho en c y estoy intentando pasarlo a c++, para aprender además del beneficio que da el lenguaje. Estoy iniciándome aún en c++ sin embargo ya tengo conocimientos de Java y .net
El problema con el me encuentro me parece que es muy simple y de novato, pero yo no lo doy resuelto.
El printf que muestra el ancho y el alto del mapa funciona bien, es decir, muestra los datos correctos. El problema viene cuando se muestran los datos con printMap puesto que debería de obtener:
0 0 0 0 -> Para el primer printMap
51 0 0 0 -> Para el segundo printMap
Y sin embargo lo que obtengo es:
2 2 2 2 -> Para el primer printMap
51 51 51 51 -> Para el segundo printMap
Main
Código C++:
Ver original#include <cstdlib>
#include <stdio.h>
#include <windows.h>
#include "Map.h"
#include "StructureDAO.h"
using namespace std;
int main(int argc, char** argv) {
Map m;
StructureDAO d;
m.SetWidth(2);
m.SetHeight(2);
if(!(m.initMap())){
printf("No se ha podido inicializar el mapa\n"); }
else{
printf("Tamaño: %dx%d\n",m.
GetWidth(),m.
GetHeight()); m.printMap();
if(!(m.addStructure(0,0,d.getStructureById(Flower1)))){
printf("No se ha añadido la estructura\n"); }
else{
m.printMap();
}
}
return 0;
}
Map.cpp
Código C++:
Ver original#include "Map.h"
#include "Structure.h"
#include <stdio.h>
#include <stdlib.h>
Map::Map() {
this->id = NOTHING;
this->width = NOTHING;
this->height = NOTHING;
this->x = NOTHING;
this->y = NOTHING;
this->cells = NULL;
}
Map::Map(const Map& orig) {
}
Map::~Map() {
}
int Map::GetFloor() const {
}
void Map
::SetFloor(int floor) { }
int Map::GetHeight() const {
return height;
}
void Map::SetHeight(int height) {
this->height = height;
}
int Map::GetId() const {
return id;
}
void Map::SetId(int id) {
this->id = id;
}
MapCell* Map::GetCells() const {
return cells;
}
void Map::SetCells(MapCell* cells) {
this->cells = cells;
}
int Map::GetWidth() const {
return width;
}
void Map::SetWidth(int width) {
this->width = width;
}
int Map::GetX() const {
return x;
}
void Map::SetX(int x) {
this->x = x;
}
int Map::GetY() const {
return y;
}
void Map::SetY(int y) {
this->y = y;
}
bool Map::initMap() {
if(this->width==NOTHING || this->height==NOTHING);
else{
int i=0;
this
->cells
= (MapCell
*)malloc(this
->width
*this
->height
*sizeof(MapCell
)); MapCell cell;
for(i=0;i<this->width*this->height;i++){
this->cells[i] = cell;
}
return true;
}
return false;
}
bool Map::addStructure(int x, int y, Structure structure){
if(this->GetCells() == NULL){
return false;
}
else{
int i=y*this->width+x;
/*if(this->cells[i].GetStructure().GetId()!=NOTHING){
this->cells[i].SetForeground(this->cells[i].GetStructure().GetId());
}*/
this->cells[i].SetStructure(structure);
printf("aaa:%d ",this
->cells
[0].
GetStructure().
GetId()); return true;
}
}
MapCell Map::getCell(int x, int y){
return this->cells[y*this->width+x];
}
MapCell Map::getCellById(int x, int y){
return this->cells[x*this->width+y];
}
bool Map::printMap(){
int i,j;
for(i=0;i<this->height;i++){
for(j=0;j<this->width;j++){
printf("%d ",this
->cells
[i
*this
->width
+j
].
GetStructure().
GetId()); }
}
}
StructureDAO.cpp
Código C++:
Ver original#include "StructureDAO.h"
#include "Structure.h"
StructureDAO::StructureDAO() {
}
StructureDAO::StructureDAO(const StructureDAO& orig) {
}
StructureDAO::~StructureDAO() {
}
Structure StructureDAO::getStructureById(int id){
Structure structure;
switch(id){
case Grass:
structure.SetId(Grass);
structure.SetState(sTRANSPARENT);
structure.SetWidth(1);
structure.SetHeight(1);
break;
case Post:
structure.SetId(Post);
structure.SetState(sSOLID);
structure.SetWidth(1);
structure.SetHeight(1);
break;
case Flower1:
structure.SetId(Flower1);
structure.SetState(sTRANSPARENT);
structure.SetWidth(1);
structure.SetHeight(1);
break;
default:
structure.SetId(NOTHING);
structure.SetState(sTRANSPARENT);
structure.SetWidth(0);
structure.SetHeight(0);
break;
}
return structure;
}
MapCell.cpp
Código C++:
Ver original#include "MapCell.h"
#include "Structure.h"
#include "MapEntry.h"
MapCell::MapCell() {
this->structure.SetId(NOTHING);
this->foreground = NOTHING;
this->decoration = NOTHING;
this->entry.SetId(NOTHING);
}
MapCell::MapCell(const MapCell& orig) {
}
MapCell::~MapCell() {
}
int MapCell::GetDecoration() const {
return decoration;
}
void MapCell::SetDecoration(int decoration) {
this->decoration = decoration;
}
MapEntry MapCell::GetEntry() const {
return entry;
}
void MapCell::SetEntry(MapEntry entry) {
this->entry = entry;
}
int MapCell::GetForeground() const {
return foreground;
}
void MapCell::SetForeground(int foreground) {
this->foreground = foreground;
}
Structure MapCell::GetStructure() const {
return structure;
}
void MapCell::SetStructure(Structure structure) {
this->structure = structure;
}
Structure.cpp
Código C++:
Ver original#include "Structure.h"
Structure::Structure() {
}
Structure::Structure(const Structure& orig) {
}
Structure::~Structure() {
}
int Structure::GetHeight() const {
return height;
}
void Structure::SetHeight(int height) {
this->height = height;
}
int Structure::GetId() const {
return id;
}
void Structure::SetId(int id) {
this->id = id;
}
int Structure::GetState() const {
return state;
}
void Structure::SetState(int state) {
this->state = state;
}
int Structure::GetWidth() const {
return width;
}
void Structure::SetWidth(int width) {
this->width = width;
}
Un saludo y muchas gracias por adelantado. Cualquier cosa que no se entienda, que supongo que habrá más de una, decirme.