Problems when deploying the template to the TAD

0

In the exercise, ask to transform a non-generic TAD matriz , to a generic by template .

In theory only you have to touch the header ( matriz.hpp ) and matriz.cpp . The project has two other files, prueba.cpp (test the operations of the TAD) and matriz_io.cpp (input / output).

Well, after adding all the templates to the operations (in matriz.hpp and matriz.cpp ), it turns out that I miss 61 red errors, all coming from these last two files ( prueba.cpp and prueba.cpp ).

I will put examples of pieces of code to make it shorter, because even if they are 61 they are really 2 or 3 errors multiplied in several parts.

  

matrix_io.cpp (ALL THE CODE YOU HAVE)

std::ostream& operator<<(std::ostream& os, Matriz& m)
{
  os << m.filas() << " " << m.columnas() << std::endl;
  os << std::setprecision(4) << std::fixed;
  for(int i=1; i <= m.filas(); i++)
  {
    for(int j=1; j <= m.columnas(); j++)
    {
      os << m.valor(i,j) << " ";
    }
    os << std::endl;
  }

  return os;
}

std::istream& operator>>(std::istream& is, Matriz& m)
{
  int filas, columnas;
  float v;

  is >> filas >> columnas;
  for (int i=1; i<=filas; i++)
  {
    for (int j=1; j<=columnas; j++)
    {
      is >> v;
      m.asignar(i,j,v);
    }
  }

  return is;
}
     
    

Errors : 'Matrix' is not a type      std :: ostream & operator < < (std :: ostream & os, Matrix & m)

         

This error occurs several times (with filas , columnas , asignar and valor ): > > request for member 'rows' in 'm', which is of non-class type 'int'        os < < m.filas () < < "" < < m.columns () < < std :: endl;

         

other: ambiguous overload for 'operator> > (operand types are     'std :: istream {aka std :: basic_istream}' and 'int') is > >     rows > > columns;

         

and another: no match for 'operator (operand types are 'std :: ostream     {aka std :: basic_ostream} 'and' Matrix ')
    std :: cout < < m;

  

In the following file all errors are unleashed by a single one:

  

prueba.cpp (Complete code)

#include "matriz.hpp"

#define Elemento float

// Descomentar cuando implementemos la template de Matriz
#define MatrizPrueba Matriz<Elemento, 3, 3>

// Comentar cuando implementemos la template de Matriz
//#define MatrizPrueba Matriz

void probarCrearMatriz()
{
  MatrizPrueba m;
  std::cout << m;
}

void probarIdentidad()
{
  MatrizPrueba m;
  m.identidad();
  std::cout << m;
}

void probarUnos()
{
  MatrizPrueba m;
  m.unos();
  std::cout << m;
}

void probarCeros()
{
  MatrizPrueba m;
  m.unos();
  m.ceros();
  std::cout << m;
}

void probarAsignar(std::istream& is)
{
  MatrizPrueba m;
  int i,j;
  Elemento v;

  is >> i >> j >> v;
  m.asignar(i, j, v);
  std::cout <<  m;
}

void probarValor(std::istream& is)
{
  MatrizPrueba m;
  int i,j;

  is >> i >> j;
  is >> m;
  std::cout << std::fixed << std::setprecision(4);
  std::cout << m.valor(i,j);
}

void probarMultiplicar(std::istream& is)
{
  MatrizPrueba m1, m2, m3;

  is >> m1;
  is >> m2;

  m1.multiplicar(m2, m3);

  std::cout << m3;
}

void probarSumar(std::istream& is)
{
  MatrizPrueba m1, m2, m3;

  is >> m1;
  is >> m2;

  m1.sumar(m2, m3);

  std::cout << m3;
}

void probarRestar(std::istream& is)
{
  MatrizPrueba m1, m2, m3;

  is >> m1;
  is >> m2;

  m1.restar(m2, m3);

  std::cout << m3;
}

void probarTrasponer(std::istream& is)
{
  MatrizPrueba m1, m2;

  is >> m1;

  m1.trasponer(m2);

  std::cout << m2;
}

Elemento cuadrado(Elemento e)
{
  return e*e;
}

void probarModificar(std::istream& is)
{
  MatrizPrueba m;

  is >> m;

  m.modificar(cuadrado);

  std::cout << m;
}

void probarCopiar(std::istream& is)
{
  MatrizPrueba m1, m2;

  is >> m1;
  is >> m2;

  m1 = m2;

  std::cout << m1;
}

void probarIgual(std::istream& is)
{
  MatrizPrueba m1, m2;

  is >> m1;
  is >> m2;

  if (m1 == m2)
  {
    std::cout << "IGUALES";
  }
  else
  {
    std::cout << "DIFERENTES";
  }
}

void probarDestruir(std::istream& is)
{
  MatrizPrueba m;

  is >> m;
}

int main()
{
  char opcion;

  // Lectura de la operaci�n a probar
  std::cin >> opcion;

  try
  {
    switch(opcion)
    {
      case 'c':  probarCrearMatriz(); break;
      case 'i':  probarIdentidad(); break;
      case 'u':  probarUnos(); break;
      case 'z':  probarCeros(); break;
      case 'a':  probarAsignar(std::cin);  break;
      case 'v':  probarValor(std::cin); break;
      case 'm':  probarMultiplicar(std::cin); break;
      case 'r':  probarRestar(std::cin); break;
      case 's':  probarSumar(std::cin); break;
      case 't':  probarTrasponer(std::cin); break;
      case 'o':  probarModificar(std::cin); break;
      case 'C':  probarCopiar(std::cin); break;
      case '=':  probarIgual(std::cin); break;
      case 'D':  probarDestruir(std::cin); break;
    }
  }
  catch (std::exception const& excepcion)
  {
    std::cout << "EXCEPCION GENERADA: "  <<  excepcion.what() << std::endl;
  }
}
  

>

     
    

Main error : can not bind 'std :: ostream {aka> > std :: basic_ostream}' lvalue to 'std :: basic_ostream &'       std :: cout < < m;

  

Any idea why these errors can be produced?

PS: Complete code (None gives errors, but as much as possible is that the errors are here , since that's the only thing I have to modify):

'matriz.hpp'

#ifndef MATRIZ_HPP
#define MATRIZ_HPP

#include <stdexcept>
#include <iostream>
#include <iomanip>

template<typename E, int F, int C>
class Matriz
{
public:

  // Definición de las clases de excepciones
  class ECoordenadasIncorrectas: public std::runtime_error
  {
  public:
    ECoordenadasIncorrectas(const std::string& w = "ECoordenadasIncorrectas"): std::runtime_error(w) {}
  };

  // Declaración de la interfaz del TAD Matriz<E,F,C>

  /**
   * POST: 'm' tiene todos sus elementos a cero.
   * COMPLEJIDAD: O(F*C)
   */
  Matriz();

  /** Destructor de la variable de tipo Matriz */
  ~Matriz();

  /**
   * POST: Establece los valores de la matriz actual con la matriz identidad.
   */
  void identidad();
  void ceros();
  void unos();
  void multiplicar(Matriz m, Matriz& salida);
  void sumar(Matriz m, Matriz& salida);
  void restar(Matriz m, Matriz& salida);
  void trasponer(Matriz& salida);
  int filas()const;
  int columnas()const;

  /**
   * POST: 'valor(i,j)' es el elemento almacenado en la celda (i,j) de la matriz 'm'.
   *       El rango válido de las coordenadas de la matriz es [1,F] y [1, C] (como en álgebra).
   * EXCEPCIONES: ECoordenadasIncorrectas si 'i' o 'j' están fuera de rango.
   */
  E valor(int i, int j);

  /**
   * POST: valor('i','j') = 'v'
   *       El rango válido de las coordenadas de la matriz es [1,F] y [1, C] (como en álgebra).
   * EXCEPCIONES: ECoordenadasIncorrectas si 'i' o 'j' están fuera de rango.
   */
  void asignar(int i, int j, E v);

  /**
   * POST: Modifica todos los elementos de la matriz utilizando la función
   *       "float modifica_elemento(float e)" que se pasa como parámetro.
   */
  template <typename ModificaElemento>
  void modificar(ModificaElemento modifica_elemento);

  /**
   * Operador de asignación
   *
   * NOTA IMPORTANTE:
   * Esta operación será necesaria cuando la implementación del TAD utilice memoria
   * dinámica. En este caso no será estrictamente necesaria dado que el operador de
   * asignación estándar "=" funciona con dos variables de tipo Matriz.
   * En cualquier caso estaremos siempre en el lado seguro si implementamos esta operación
   * para todo TAD.
   */
  Matriz& operator=(Matriz& m);

  /** Constructor de copia */
  Matriz(Matriz& m);

  /**
   * Operadores de comparación
   *
   * NOTA IMPORTANTE:
   * Esta operación será necesaria cuando la implementación del TAD utilice memoria
   * dinámica o cuando el == o el != estándar no funcione (compara byte a byte).
   */
  bool operator==(Matriz& m);
  bool operator!=(Matriz& m);



private:
  E elementos_[F][C];
};

// En las templates (TADs genéricos) hacemos la inclusión al revés el .cpp en el .hpp (para que
// al incluir el matrices.hpp vaya todo el código y se pueda hacer la instanciación de la
// template).
#include "matriz.cpp"
#include "matriz_io.cpp"

#endif // MATRIZ_HPP

matriz.cpp

#include <limits>
#include <cmath>


// COMPLEJIDAD: O(F*C)
template<typename E, int F, int C>
Matriz<E,F,C>::Matriz()
{
  for(int i=0; i<F; i++)
  {
    for(int j=0; j<C; j++)
    {
      elementos_[i][j] = 0; // Valor int, C++ lo convierte automáticamente a E.
    }
  }
}
template<typename E, int F, int C>
Matriz<E,F,C>::~Matriz()
{
 // En este caso no tenemos memoria dinámica y no tenemos nada que hacer.
}


// COMPLEJIDAD: O(F*C)
template<typename E, int F, int C>
void Matriz<E,F,C>::identidad()
{
  for(int i=0; i<F; i++)
  {
    for(int j=0; j<C; j++)
    {
      if (i==j)
      {
        elementos_[i][j] = 1; // Valor int, C++ lo convierte automáticamente a E.
      }
      else
      {
        elementos_[i][j] = 0; // Valor int, C++ lo convierte automáticamente a E.
      }
    }
  }
}
template<typename E, int F, int C>
void Matriz<E,F,C>::ceros()
{
    for(int i=0; i<F; i++)
    {
        for(int j=0; j<C; j++)
        {
            elementos_[i][j] = 0;
        }
    }
}

template<typename E, int F, int C>
E Matriz<E,F,C>::valor(int i, int j)
{
  if ((i<1) || (i>F) || (j<1) || (j>C))
  {
    throw ECoordenadasIncorrectas();
  }
  else
  {
    return elementos_[i-1][j-1];
  }
}
template<typename E, int F, int C>
void Matriz<E,F,C>::asignar(int i, int j, E v)
{
  if ((i<1) || (i>F) || (j<1) || (j>C))
  {
    throw ECoordenadasIncorrectas();
  }
  else
  {
    elementos_[i-1][j-1] = v;
  }
}

template<typename E, int F, int C>
template <typename ModificaElemento>
void Matriz<E,F,C>::modificar(ModificaElemento modifica_elemento)
{
  for(int i=0; i<F; i++)
  {
    for(int j=0; j<C; j++)
    {
      elementos_[i][j] = modifica_elemento(elementos_[i][j]);
    }
  }
}

template<typename E, int F, int C>
Matriz<E,F,C>& Matriz<E,F,C>::operator=(Matriz<E,F,C>& m)
{
  for(int i=0; i<F; i++)
  {
    for(int j=0; j<C; j++)
    {
      elementos_[i][j] = m.elementos_[i][j];
    }
  }

  return (*this);
}

template<typename E, int F, int C>
Matriz<E,F,C>::Matriz(Matriz<E,F,C>& m)
{
  (*this) = m;
}

template<typename E, int F, int C>
bool Matriz<E,F,C>::operator==(Matriz<E,F,C>& m)
{
  int i = 0;
  int j = 0;
  bool iguales = true;

  while ((i<F) && iguales)
  {
    j = 0;
    while ((j<C) && iguales)
    {
      iguales = std::abs(elementos_[i][j] - m.elementos_[i][j]) < std::numeric_limits<float>::epsilon();
      j++;
    }
    i++;
  }

  return iguales;
}

template<typename E, int F, int C>
bool Matriz<E,F,C>::operator!=(Matriz<E,F,C>& m)
{
  return !(*this == m);
}
//EJERCICIO PARTE: IMPLEMENTACION


template<typename E, int F, int C>
void Matriz<E,F,C>::unos()
{
    for(int i=0; i<F; i++)
    {
        for(int j=0; j<C; j++)
        {
            elementos_[i][j]=1;
        }
    }
}
template<typename E, int F, int C>
int Matriz<E,F,C>::columnas() const
{

    return (C);
}
template<typename E, int F, int C>
int Matriz<E,F,C>::filas() const
{

    return (F);
}
template<typename E, int F, int C>
void Matriz<E,F,C>::sumar(Matriz<E,F,C> m, Matriz<E,F,C>& salida)
{
    for(int i=0; i<F; i++)
    {
        for(int j=0; j<C; j++)
        {
            salida.elementos_[i][j]=elementos_[i][j]+m.elementos_[i][j];
        }
    }

}
template<typename E, int F, int C>
void Matriz<E,F,C>::restar(Matriz<E,F,C> m, Matriz<E,F,C>& salida)
{
    for(int i=0; i<F; i++)
    {
        for(int j=0; j<C; j++)
        {
            salida.elementos_[i][j]=elementos_[i][j]-m.elementos_[i][j];
        }
    }
}
template<typename E, int F, int C>
void Matriz<E,F,C>::multiplicar(Matriz<E,F,C> m, Matriz<E,F,C>& salida)
{
    for(int i=0; i<F; i++)
    {
        for(int j=0; j<C; j++)
        {
            for(int k=0; k<C;k++)
            {
                salida.elementos_[i][j]+=elementos_[i][k]*m.elementos_[k][j];
            }
        }
    }
}
template<typename E, int F, int C>
void Matriz<E,F,C>::trasponer(Matriz<E,F,C>& salida)
{
    for(int i=0; i<F; i++)
    {
        for(int j=0; j<C; j++)
        {
            salida.elementos_[i][j]=elementos_[j][i];
        }
    }
}

All idea will be welcome.

    
asked by AguaSal 22.06.2017 в 20:05
source

2 answers

0

Finally I have managed to solve the error, for those who have the same or similar and want to see it, I put here all the code and the modified parts marked.

  

Modified code   "Everything was wrong here: array_io.cpp (already corrected)"

template<typename E, int filas, int columnas>
std::ostream& operator<<(std::ostream& os, Matriz<E,filas,columnas>& m)
{
  os << m.filas() << " " << m.columnas() << std::endl;
  os << std::setprecision(4) << std::fixed;
  for(int i=1; i <= m.filas(); i++)
  {
    for(int j=1; j <= m.columnas(); j++)
    {
      os << m.valor(i,j) << " ";
    }
    os << std::endl;
  }

  return os;
}
template<typename E, int F, int C>
std::istream& operator>>(std::istream& is, Matriz<E,F,C>& m)
{
    int filas, columnas;

  E v;

  is >> filas >> columnas;
  for (int i=1; i<=filas; i++)
  {
    for (int j=1; j<=columnas; j++)
    {
      is >> v;
      m.asignar(i,j,v);
    }
  }

  return is;
}

The rest of the code is practically untouched, simple mistakes.

prueba.cpp:

#include "matriz.hpp"

#define Elemento float

// Descomentar cuando implementemos la template de Matriz
#define MatrizPrueba Matriz<Elemento, 3, 3>

// Comentar cuando implementemos la template de Matriz
//#define MatrizPrueba Matriz


void probarCrearMatriz()
{
  MatrizPrueba m;
  std::cout << m;
}

void probarIdentidad()
{
  MatrizPrueba m;
  m.identidad();
  std::cout << m;
}

void probarUnos()
{
  MatrizPrueba m;
  m.unos();
  std::cout << m;
}

void probarCeros()
{
  MatrizPrueba m;
  m.unos();
  m.ceros();
  std::cout << m;
}

void probarAsignar(std::istream& is)
{
  MatrizPrueba m;
  int i,j;
  Elemento v;

  is >> i >> j >> v;
  m.asignar(i, j, v);
  std::cout <<  m;
}

void probarValor(std::istream& is)
{
  MatrizPrueba m;
  int i,j;

  is >> i >> j;
  is >> m;
  std::cout << std::fixed << std::setprecision(4);
  std::cout << m.valor(i,j);
}

void probarMultiplicar(std::istream& is)
{
  MatrizPrueba m1, m2, m3;

  is >> m1;
  is >> m2;

  m1.multiplicar(m2, m3);

  std::cout << m3;
}

void probarSumar(std::istream& is)
{
  MatrizPrueba m1, m2, m3;

  is >> m1;
  is >> m2;

  m1.sumar(m2, m3);

  std::cout << m3;
}

void probarRestar(std::istream& is)
{
  MatrizPrueba m1, m2, m3;

  is >> m1;
  is >> m2;

  m1.restar(m2, m3);

  std::cout << m3;
}

void probarTrasponer(std::istream& is)
{
  MatrizPrueba m1, m2;

  is >> m1;

  m1.trasponer(m2);

  std::cout << m2;
}

Elemento cuadrado(Elemento e)
{
  return e*e;
}

void probarModificar(std::istream& is)
{
  MatrizPrueba m;

  is >> m;

  m.modificar(cuadrado);

  std::cout << m;
}

void probarCopiar(std::istream& is)
{
  MatrizPrueba m1, m2;

  is >> m1;
  is >> m2;

  m1 = m2;

  std::cout << m1;
}

void probarIgual(std::istream& is)
{
  MatrizPrueba m1, m2;

  is >> m1;
  is >> m2;

  if (m1 == m2)
  {
    std::cout << "IGUALES";
  }
  else
  {
    std::cout << "DIFERENTES";
  }
}

void probarDestruir(std::istream& is)
{
  MatrizPrueba m;

  is >> m;
}

int main()
{
  char opcion;

  // Lectura de la operaci?n a probar
  std::cin >> opcion;

  try
  {
    switch(opcion)
    {
      case 'c':  probarCrearMatriz(); break;
      case 'i':  probarIdentidad(); break;
      case 'u':  probarUnos(); break;
      case 'z':  probarCeros(); break;
      case 'a':  probarAsignar(std::cin);  break;
      case 'v':  probarValor(std::cin); break;
      case 'm':  probarMultiplicar(std::cin); break;
      case 'r':  probarRestar(std::cin); break;
      case 's':  probarSumar(std::cin); break;
      case 't':  probarTrasponer(std::cin); break;
      case 'o':  probarModificar(std::cin); break;
      case 'C':  probarCopiar(std::cin); break;
      case '=':  probarIgual(std::cin); break;
      case 'D':  probarDestruir(std::cin); break;
    }
  }
  catch (std::exception const& excepcion)
  {
    std::cout << "EXCEPCION GENERADA: "  <<  excepcion.what() << std::endl;
  }
}

matriz.cpp

#include <limits>
#include <cmath>


// COMPLEJIDAD: O(F*C)
template<typename E, int F, int C>
Matriz<E,F,C>::Matriz()
{
  for(int i=0; i<F; i++)
  {
    for(int j=0; j<C; j++)
    {
      elementos_[i][j] = 0; // Valor int, C++ lo convierte autom�ticamente a E.
    }
  }
}
template<typename E, int F, int C>
Matriz<E,F,C>::~Matriz()
{
 // En este caso no tenemos memoria din�mica y no tenemos nada que hacer.
}


// COMPLEJIDAD: O(F*C)
template<typename E, int F, int C>
void Matriz<E,F,C>::identidad()
{
  for(int i=0; i<F; i++)
  {
    for(int j=0; j<C; j++)
    {
      if (i==j)
      {
        elementos_[i][j] = 1; // Valor int, C++ lo convierte autom�ticamente a E.
      }
      else
      {
        elementos_[i][j] = 0; // Valor int, C++ lo convierte autom�ticamente a E.
      }
    }
  }
}
template<typename E, int F, int C>
void Matriz<E,F,C>::ceros()
{
    for(int i=0; i<F; i++)
    {
        for(int j=0; j<C; j++)
        {
            elementos_[i][j] = 0;
        }
    }
}

template<typename E, int F, int C>
E Matriz<E,F,C>::valor(int i, int j)
{
  if ((i<1) || (i>F) || (j<1) || (j>C))
  {
    throw ECoordenadasIncorrectas();
  }
  else
  {
    return elementos_[i-1][j-1];
  }
}
template<typename E, int F, int C>
void Matriz<E,F,C>::asignar(int i, int j, float v)
{
  if ((i<1) || (i>F) || (j<1) || (j>C))
  {
    throw ECoordenadasIncorrectas();
  }
  else
  {
    elementos_[i-1][j-1] = v;
  }
}

template<typename E, int F, int C>
template <typename ModificaElemento>
void Matriz<E,F,C>::modificar(ModificaElemento modifica_elemento)
{
  for(int i=0; i<F; i++)
  {
    for(int j=0; j<C; j++)
    {
      elementos_[i][j] = modifica_elemento(elementos_[i][j]);
    }
  }
}

template<typename E, int F, int C>
Matriz<E,F,C>& Matriz<E,F,C>::operator=(Matriz<E,F,C>& m)
{
  for(int i=0; i<F; i++)
  {
    for(int j=0; j<C; j++)
    {
      elementos_[i][j] = m.elementos_[i][j];
    }
  }

  return (*this);
}

template<typename E, int F, int C>
Matriz<E,F,C>::Matriz(Matriz<E,F,C>& m)
{
  (*this) = m;
}

template<typename E, int F, int C>
bool Matriz<E,F,C>::operator==(Matriz<E,F,C>& m)
{
  int i = 0;
  int j = 0;
  bool iguales = true;

  while ((i<F) && iguales)
  {
    j = 0;
    while ((j<C) && iguales)
    {
      iguales = std::abs(elementos_[i][j] - m.elementos_[i][j]) < std::numeric_limits<float>::epsilon();
      j++;
    }
    i++;
  }

  return iguales;
}

template<typename E, int F, int C>
bool Matriz<E,F,C>::operator!=(Matriz<E,F,C>& m)
{
  return !(*this == m);
}
//EJERCICIO PARTE: IMPLEMENTACION


template<typename E, int F, int C>
void Matriz<E,F,C>::unos()
{
    for(int i=0; i<F; i++)
    {
        for(int j=0; j<C; j++)
        {
            elementos_[i][j]=1;
        }
    }
}
template<typename E, int F, int C>
int Matriz<E,F,C>::columnas()
{

    return (C);
}
template<typename E, int F, int C>
int Matriz<E,F,C>::filas()
{

    return (F);
}
template<typename E, int F, int C>
void Matriz<E,F,C>::sumar(Matriz m, Matriz& salida)
{
    for(int i=0; i<F; i++)
    {
        for(int j=0; j<C; j++)
        {
            salida.elementos_[i][j]=elementos_[i][j]+m.elementos_[i][j];
        }
    }

}
template<typename E, int F, int C>
void Matriz<E,F,C>::restar(Matriz m, Matriz& salida)
{
    for(int i=0; i<F; i++)
    {
        for(int j=0; j<C; j++)
        {
            salida.elementos_[i][j]=elementos_[i][j]-m.elementos_[i][j];
        }
    }
}
template<typename E, int F, int C>
void Matriz<E,F,C>::multiplicar(Matriz m, Matriz& salida)
{
    for(int i=0; i<F; i++)
    {
        for(int j=0; j<C; j++)
        {
            for(int k=0; k<C;k++)
            {
                salida.elementos_[i][j]+=elementos_[i][k]*m.elementos_[k][j];
            }
        }
    }
}
template<typename E, int F, int C>
void Matriz<E,F,C>::trasponer(Matriz& salida)
{
    for(int i=0; i<F; i++)
    {
        for(int j=0; j<C; j++)
        {
            salida.elementos_[i][j]=elementos_[j][i];
        }
    }
}

matriz.hpp

#ifndef MATRIZ_HPP
#define MATRIZ_HPP

#include <stdexcept>
#include <iostream>
#include <iomanip>

//#define F 3
//#define C 3

template<typename E, int F, int C>
class Matriz
{
public:

  // Definici�n de las clases de excepciones
  class ECoordenadasIncorrectas: public std::runtime_error
  {
  public:
    ECoordenadasIncorrectas(const std::string& w = "ECoordenadasIncorrectas"): std::runtime_error(w) {}
  };

  // Declaraci�n de la interfaz del TAD Matriz<E,F,C>

  /**
   * POST: 'm' tiene todos sus elementos a cero.
   * COMPLEJIDAD: O(F*C)
   */
  Matriz();

  /** Destructor de la variable de tipo Matriz */
  ~Matriz();

  /**
   * POST: Establece los valores de la matriz actual con la matriz identidad.
   */
  void identidad();
  void ceros();
  void unos();
  void multiplicar(Matriz m, Matriz& salida);
  void sumar(Matriz m, Matriz& salida);
  void restar(Matriz m, Matriz& salida);
  void trasponer(Matriz& salida);
  int filas();
  int columnas();

  /**
   * POST: 'valor(i,j)' es el elemento almacenado en la celda (i,j) de la matriz 'm'.
   *       El rango v�lido de las coordenadas de la matriz es [1,F] y [1, C] (como en �lgebra).
   * EXCEPCIONES: ECoordenadasIncorrectas si 'i' o 'j' est�n fuera de rango.
   */
  E valor(int i, int j);

  /**
   * POST: valor('i','j') = 'v'
   *       El rango v�lido de las coordenadas de la matriz es [1,F] y [1, C] (como en �lgebra).
   * EXCEPCIONES: ECoordenadasIncorrectas si 'i' o 'j' est�n fuera de rango.
   */
  void asignar(int i, int j, float v);

  /**
   * POST: Modifica todos los elementos de la matriz utilizando la funci�n
   *       "float modifica_elemento(float e)" que se pasa como par�metro.
   */
  template <typename ModificaElemento>
  void modificar(ModificaElemento modifica_elemento);

  /**
   * Operador de asignaci�n
   *
   * NOTA IMPORTANTE:
   * Esta operaci�n ser� necesaria cuando la implementaci�n del TAD utilice memoria
   * din�mica. En este caso no ser� estrictamente necesaria dado que el operador de
   * asignaci�n est�ndar "=" funciona con dos variables de tipo Matriz.
   * En cualquier caso estaremos siempre en el lado seguro si implementamos esta operaci�n
   * para todo TAD.
   */
  Matriz& operator=(Matriz& m);

  /** Constructor de copia */
  Matriz(Matriz& m);

  /**
   * Operadores de comparaci�n
   *
   * NOTA IMPORTANTE:
   * Esta operaci�n ser� necesaria cuando la implementaci�n del TAD utilice memoria
   * din�mica o cuando el == o el != est�ndar no funcione (compara byte a byte).
   */
  bool operator==(Matriz& m);
  bool operator!=(Matriz& m);
  //EJERCICIO PARTE: ESPECIFICACION


private:
  E elementos_[F][C];
};

// En las templates (TADs gen�ricos) hacemos la inclusi�n al rev�s el .cpp en el .hpp (para que
// al incluir el matrices.hpp vaya todo el c�digo y se pueda hacer la instanciaci�n de la
// template).
#include "matriz.cpp"
#include "matriz_io.cpp"

#endif // MATRIZ_HPP
  

Error summary:    I just had to add templates also to   operators overloaded in matriz_io.cpp .

    
answered by 25.06.2017 / 03:56
source
-1

Good afternoon; I have a very similar problem and that is why I also mention it here. I want to make a cout exit with a TAD Monomio in the following way:

Monomio c;
cout<<c;

For this I have summarized the following code:

Monomio.h file

class Monomio {
    public:
    .....
    std::ostream& operator<<(std::ostream&, const Monomio&);
    ....
    private:
    double C;
    int expX;
    int expY;

};

Monomio.cpp file

...

    std::ostream& Monomio::operator<<(std::ostream& os, const Monomio& mo){
        os<<mo.C<<"X^"<<mo.expX<<"Y^"<<mo.expY;
        return os;

}
....

Error obtained: Monomio.h: 33: 59: error: 'std :: ostream & Monomio :: operator

answered by 22.06.2017 в 20:39