Foros del Web » Programación para mayores de 30 ;) » C/C++ »

Problema con C++ en la llamada a procedimientos

Estas en el tema de Problema con C++ en la llamada a procedimientos en el foro de C/C++ en Foros del Web. 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 ...
  #1 (permalink)  
Antiguo 09/06/2012, 10:30
 
Fecha de Ingreso: junio-2012
Mensajes: 1
Antigüedad: 11 años, 10 meses
Puntos: 0
Pregunta Problema con C++ en la llamada a procedimientos

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
  1. #include <cstdlib>
  2. #include <stdio.h>
  3. #include <windows.h>
  4.  
  5. #include "Map.h"
  6. #include "StructureDAO.h"
  7.  
  8. using namespace std;
  9.  
  10. int main(int argc, char** argv) {
  11.     Map m;
  12.     StructureDAO d;
  13.     m.SetWidth(2);
  14.     m.SetHeight(2);
  15.     if(!(m.initMap())){
  16.         printf("No se ha podido inicializar el mapa\n");
  17.         exit(1);
  18.     }
  19.     else{
  20.         printf("Tamaño: %dx%d\n",m.GetWidth(),m.GetHeight());
  21.         m.printMap();
  22.         if(!(m.addStructure(0,0,d.getStructureById(Flower1)))){
  23.             printf("No se ha añadido la estructura\n");
  24.             exit(2);
  25.         }
  26.         else{
  27.             m.printMap();
  28.         }
  29.     }
  30.    
  31.     free(m.GetCells());
  32.     return 0;
  33. }


Map.cpp
Código C++:
Ver original
  1. #include "Map.h"
  2. #include "Structure.h"
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. Map::Map() {
  7.     this->id = NOTHING;
  8.     this->width = NOTHING;
  9.     this->height = NOTHING;
  10.     this->x = NOTHING;
  11.     this->y = NOTHING;
  12.     this->floor = NOTHING;
  13.     this->cells = NULL;
  14. }
  15.  
  16. Map::Map(const Map& orig) {
  17. }
  18.  
  19. Map::~Map() {
  20. }
  21.  
  22. int Map::GetFloor() const {
  23.     return floor;
  24. }
  25.  
  26. void Map::SetFloor(int floor) {
  27.     this->floor = floor;
  28. }
  29.  
  30. int Map::GetHeight() const {
  31.     return height;
  32. }
  33.  
  34. void Map::SetHeight(int height) {
  35.     this->height = height;
  36. }
  37.  
  38. int Map::GetId() const {
  39.     return id;
  40. }
  41.  
  42. void Map::SetId(int id) {
  43.     this->id = id;
  44. }
  45.  
  46. MapCell* Map::GetCells() const {
  47.     return cells;
  48. }
  49.  
  50. void Map::SetCells(MapCell* cells) {
  51.     this->cells = cells;
  52. }
  53.  
  54. int Map::GetWidth() const {
  55.     return width;
  56. }
  57.  
  58. void Map::SetWidth(int width) {
  59.     this->width = width;
  60. }
  61.  
  62. int Map::GetX() const {
  63.     return x;
  64. }
  65.  
  66. void Map::SetX(int x) {
  67.     this->x = x;
  68. }
  69.  
  70. int Map::GetY() const {
  71.     return y;
  72. }
  73.  
  74. void Map::SetY(int y) {
  75.     this->y = y;
  76. }
  77.  
  78.  
  79. bool Map::initMap() {
  80.     if(this->width==NOTHING || this->height==NOTHING);
  81.     else{
  82.         int i=0;
  83.         this->cells = (MapCell*)malloc(this->width*this->height*sizeof(MapCell));
  84.         MapCell cell;
  85.         for(i=0;i<this->width*this->height;i++){
  86.             this->cells[i] = cell;
  87.         }
  88.         return true;
  89.     }
  90.     return false;
  91. }
  92.  
  93. bool Map::addStructure(int x, int y, Structure structure){
  94.     if(this->GetCells() == NULL){
  95.         return false;
  96.     }
  97.     else{
  98.         int i=y*this->width+x;
  99.         /*if(this->cells[i].GetStructure().GetId()!=NOTHING){
  100.             this->cells[i].SetForeground(this->cells[i].GetStructure().GetId());
  101.         }*/
  102.         this->cells[i].SetStructure(structure);
  103.         printf("aaa:%d ",this->cells[0].GetStructure().GetId());
  104.         return true;
  105.     }
  106. }
  107.  
  108. MapCell Map::getCell(int x, int y){
  109.     return this->cells[y*this->width+x];
  110. }
  111.  
  112. MapCell Map::getCellById(int x, int y){
  113.     return this->cells[x*this->width+y];
  114. }
  115.  
  116. bool Map::printMap(){
  117.     int i,j;
  118.     for(i=0;i<this->height;i++){
  119.         for(j=0;j<this->width;j++){
  120.             printf("%d ",this->cells[i*this->width+j].GetStructure().GetId());
  121.         }
  122.         printf("\n");
  123.     }
  124. }


StructureDAO.cpp
Código C++:
Ver original
  1. #include "StructureDAO.h"
  2. #include "Structure.h"
  3.  
  4. StructureDAO::StructureDAO() {
  5. }
  6.  
  7. StructureDAO::StructureDAO(const StructureDAO& orig) {
  8. }
  9.  
  10. StructureDAO::~StructureDAO() {
  11. }
  12.  
  13. Structure StructureDAO::getStructureById(int id){
  14.     Structure structure;
  15.     switch(id){
  16.         case Grass:
  17.             structure.SetId(Grass);
  18.             structure.SetState(sTRANSPARENT);
  19.             structure.SetWidth(1);
  20.             structure.SetHeight(1);
  21.             break;
  22.         case Post:
  23.             structure.SetId(Post);
  24.             structure.SetState(sSOLID);
  25.             structure.SetWidth(1);
  26.             structure.SetHeight(1);
  27.             break;
  28.         case Flower1:
  29.             structure.SetId(Flower1);
  30.             structure.SetState(sTRANSPARENT);
  31.             structure.SetWidth(1);
  32.             structure.SetHeight(1);
  33.             break;
  34.         default:
  35.             structure.SetId(NOTHING);
  36.             structure.SetState(sTRANSPARENT);
  37.             structure.SetWidth(0);
  38.             structure.SetHeight(0);
  39.             break;
  40.     }
  41.     return structure;
  42. }

MapCell.cpp
Código C++:
Ver original
  1. #include "MapCell.h"
  2. #include "Structure.h"
  3. #include "MapEntry.h"
  4.  
  5. MapCell::MapCell() {
  6.     this->structure.SetId(NOTHING);
  7.     this->foreground = NOTHING;
  8.     this->decoration = NOTHING;
  9.     this->entry.SetId(NOTHING);
  10. }
  11.  
  12. MapCell::MapCell(const MapCell& orig) {
  13. }
  14.  
  15. MapCell::~MapCell() {
  16. }
  17.  
  18. int MapCell::GetDecoration() const {
  19.     return decoration;
  20. }
  21.  
  22. void MapCell::SetDecoration(int decoration) {
  23.     this->decoration = decoration;
  24. }
  25.  
  26. MapEntry MapCell::GetEntry() const {
  27.     return entry;
  28. }
  29.  
  30. void MapCell::SetEntry(MapEntry entry) {
  31.     this->entry = entry;
  32. }
  33.  
  34. int MapCell::GetForeground() const {
  35.     return foreground;
  36. }
  37.  
  38. void MapCell::SetForeground(int foreground) {
  39.     this->foreground = foreground;
  40. }
  41.  
  42. Structure MapCell::GetStructure() const {
  43.     return structure;
  44. }
  45.  
  46. void MapCell::SetStructure(Structure structure) {
  47.     this->structure = structure;
  48. }

Structure.cpp
Código C++:
Ver original
  1. #include "Structure.h"
  2.  
  3. Structure::Structure() {
  4. }
  5.  
  6. Structure::Structure(const Structure& orig) {
  7. }
  8.  
  9. Structure::~Structure() {
  10. }
  11.  
  12. int Structure::GetHeight() const {
  13.     return height;
  14. }
  15.  
  16. void Structure::SetHeight(int height) {
  17.     this->height = height;
  18. }
  19.  
  20. int Structure::GetId() const {
  21.     return id;
  22. }
  23.  
  24. void Structure::SetId(int id) {
  25.     this->id = id;
  26. }
  27.  
  28. int Structure::GetState() const {
  29.     return state;
  30. }
  31.  
  32. void Structure::SetState(int state) {
  33.     this->state = state;
  34. }
  35.  
  36. int Structure::GetWidth() const {
  37.     return width;
  38. }
  39.  
  40. void Structure::SetWidth(int width) {
  41.     this->width = width;
  42. }


Un saludo y muchas gracias por adelantado. Cualquier cosa que no se entienda, que supongo que habrá más de una, decirme.

Etiquetas: funcion, int, lenguaje, llamada, procedimientos, programa, struct
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 20:06.