Ver Mensaje Individual
  #1 (permalink)  
Antiguo 10/06/2008, 15:29
fran_tatengue
 
Fecha de Ingreso: abril-2008
Mensajes: 19
Antigüedad: 16 años, 1 mes
Puntos: 0
autodestruccion de variable entera

Hola gente hace mucho que no entraba en el foro pero no encontre otra solcuion.
Les comento que estoy haciendo un TP para la facultad y tengo que entregarlo el viernes y me surgió un problema que la verdad nunca antes me habia pasado.

El problema es el siguiente:

Tengo esta clase definida

Código HTML:
//archivo matriz.h
#ifndef MATRIZ_H
#define MATRIZ_H

class matriz
{        
    public:
        matriz();
        void set_matriz(int, int, int);
        void ver_matriz();
    
    private:
        int **matrizPtr;
        int x;
        int y;
        int p_x;                        //esta var me da problema
        int p_y;                        //esta var me da problema
};

#endif
ahora les presento el archivo con las funciones miembros, los problemas estan marcados con "*" (asterizcos)

Código HTML:
#include "matriz.h"
#include "principal.h"
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

matriz::matriz()
{
    p_x = 0;                                  ////////////  *1    //////////
    p_y = 0;
    
    cout << "p_x = " << p_x << "    p_y = " << p_y << endl;
    system("pause");
    
    principal P1;
    matrizPtr = NULL;
    
    int  i = 0, j = 0;
    
    x = P1.get_x();
    y = P1.get_y();
    
    matrizPtr = new int*[x];
    
    for(i = 0; i <= x - 1; i++)
    {
        matrizPtr[i] = new int[y];
    }
    
    i = 0;
    
    if(matrizPtr != NULL)
    {
        for(i = 0; i <= x - 1; i++)
        {
            for(j = 0; j <= y - 1; j++)
            {
                matrizPtr[i][j] = 0;
            }
        }
    }
    else
    {
        cout << "MEMORIA INSUFICIENTE" << endl;
    }
}

void matriz::set_matriz(int i, int j, int lapiz)
{     
    if(i == 1)
    {
        p_x++;
    }
    else if(i == -1)
    {
        p_x--;
    }
    
    if(j == 1)
    {
        p_y++;        
    }
    else if(j == -1)
    {
        p_y--;
    }
    
    cout << "p_x = " << p_x << "    p_y = " << p_y << endl;     ////////  *2   ///////
    system("pause");
    
    if(lapiz == 1)
    {
        matrizPtr[p_x][p_y] = 1;
    }
 
}      

void matriz::ver_matriz()
{

    for(int i = 0; i <= x - 1; i++)
        {
            for(int j = 0; j <= y - 1; j++)
            {
                cout << matrizPtr[i][j] << "  ";
            }
            cout << endl;
        }
      
    system("pause");
}  
Y esto es lo que muestra al ejecutar:




Y luego el programa muere!!!.

*1 =
Código HTML:
    //ESTO ES DENTRO DEL CONSTRUCTOR

    p_x = 0;                                 
    p_y = 0;
    
    cout << "p_x = " << p_x << "    p_y = " << p_y << endl;
    system("pause");


*2 =
Código HTML:
    //ESTO ES DENTRO DE LA FUNCION set_matriz

    cout << "p_x = " << p_x << "    p_y = " << p_y << endl;            *2 
    system("pause");


Les explico por si no entendieron en el punto *1 inicilizo las variables a 0 y las imprimo para que se vea que valen 0.
En el punto *2 vuelvo a imprimir en pantalla los valores de las varibles sin haberlas usado antes (porque nunca entre en los if) y se destruyen por completo!

¿Cual puede ser el problema?.