• Viernes 26 de Abril de 2024, 01:19

Autor Tema:  Template de Matriz  (Leído 1104 veces)

-.-JP

  • Nuevo Miembro
  • *
  • Mensajes: 9
    • Ver Perfil
Template de Matriz
« en: Viernes 3 de Junio de 2011, 01:43 »
0
Buenas!!
He implementado una template de matriz; con el objetivo de usarle en un proyecto, especificamente un juego de dados, con casillas y mas....
aparentemente todo funciona, pero tengo un problema con el toString()!  no me reconoce la funcion que tendria el objeto.

Adjunto el codigo: Espero Puedan ayudarme a Solucionarlo. MUCHAS GRACIAS

#ifndef MATRIZ_H
#define   MATRIZ_H

#include <iostream>
#include <sstream>

using namespace std;


template <class T> class matriz;

template <class T>
class matriz {
public:
    matriz();
    virtual ~matriz();
    matriz(const matriz<T>&);

    virtual unsigned int n() const;

    void agregar(T*, unsigned int, unsigned int);
    virtual T* recuperar(unsigned int, unsigned int) const;


    virtual bool validar(unsigned int, unsigned int);

    virtual string toString()const;
    virtual void eliminar(unsigned int, unsigned int);

private:
    T*** _mat;
    static const int _n;
};

// -----------------------------------------------------------//

template <class T>
matriz<T>::matriz() {
    _mat = new T**[_n];
    for (int i = _n; i >= 1; i--) {
        _mat = new T*[_n];
        for (int j = 0; j < _n; j++) {
            _mat [j] = NULL;
        }
    }
}

template <class T>
const int matriz<T>::_n = 9;

template <class T>
unsigned int matriz<T>::n() const {
    return _n;
}

template <class T>
matriz<T>::~matriz() {
    for (unsigned int i = 0; i < _n; i++) {
        for (unsigned int j = 0; j < _n; j++) {
            if (_mat[j] != NULL) {
                delete _mat[j];
            };
        }
        delete[] _mat;
    }
    delete[] _mat;
}

template <class T>
void matriz<T>::eliminar(unsigned int filas, unsigned int col) {
    _mat[filas][col] = NULL;
}

template <class T>
void matriz<T>::agregar(T* nuevo, unsigned int filas, unsigned int col) {
    if ((filas <= _n) && (col <= _n)) {
        if (!_mat[filas][col]) {
            _mat[filas][col] = nuevo;
        } else {
            _mat[filas][col] = NULL;
        }
    }
}

template <class T>
T* matriz<T>::recuperar(unsigned int filas, unsigned int col) const {
    return (filas < n() && col < n()) ? _mat[filas][col] : NULL;
}

template <class T>
bool matriz<T>::validar(unsigned int filas, unsigned int col) {
    return (filas < n() && col < n());
}

template <class T>
string matriz<T>::toString()const {
    stringstream r;
    unsigned int c = n() + 1;
    r << "n";
    for (unsigned int i = 0; i < n(); i++) {
        r << "  +---+---+---+---+---+---+---+---+---+";
        r << "n";
        c--;
        for (unsigned int j = 0; j < n(); j++) {
            if (j == 0) {
                r << c << " ";
            }
            r << "|" << " ";
     r << ((_mat[j] != NULL) ?  ? _mat[j]->toString() : " ");
            r << " ";
            if (j == n() - 1) {
                r << "| n";
            }
        }
    }

    return r.str();
}

#endif   /* MATRIZ_H */