-1

How can I solve the following errors, thanks.

[Error] 'IntercDirectoIzq' was not declared in this scope

[Error] expected primary-expression before '>' token

[Error] 'Orden' was not declared in this scope

[Error] no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream}' and 'Alumno')

Alumno.cpp

#include "Alumno.h"

#include "Arreglo.h"

#include "MetOrdena.h"

#include<iostream>

#include<stdio.h>

using namespace std;

int main()
{

Arreglo<Alumno> Escuela;
Escuela.Lectura();

IntercDirectoIzq<Alumno> Orden;
Orden.Ordena(&Escuela);

Escuela.Escribe();

if (Escuela.RegresaTam() != 0)
cout<<"Los datos del primer alumno son:"<<endl;
cout<< Escuela.RegresaValor(0);
return 0;
}

Alumno.h


#include<iostream>
#include<stdio.h>
#include<cstring>
using namespace std;

class Alumno {
private:
int Clave;
char Nombre[64];
public:
Alumno();
Alumno(int, char *);
int operator > (Alumno);
friend istream &operator >> (istream &, Alumno &);
friend ostream &operator << (ostream &, Alumno &);
};

Alumno::Alumno()
{}

Alumno::Alumno(int Cla, char Nom[])
{
Clave= Cla;
strcpy(Nombre, Nom);
}

int Alumno::operator > (Alumno ObjAl)
{
if (Clave > ObjAl.Clave)
return 1;
else
return 0;
}

istream &operator >> (istream &Lee, Alumno &ObjAl)
{
cout<<"\n\nIngrese clave del alumno: ";
Lee>>ObjAl.Clave;
cout<<"\n\nIngrese nombre del alumno: ";
Lee>>ObjAl.Nombre;
return Lee;
}

ostream &operator << (ostream &Escribe, Alumno &ObjAl)
{
Escribe<<"\n\nDatos del alumno\n";
Escribe<<"\nClave: "<<ObjAl.Clave;
Escribe<<"\nNombre: "<<ObjAl.Nombre<<"\n";
return Escribe;
}

Arreglo.h


#include<iostream>
#include<stdio.h>

#define MAX 100

using namespace std;

template <class T>
class Arreglo
{
private:
T Datos[MAX];
int Tam;
public:
Arreglo();
int RegresaTam();
T RegresaValor(int);
void AsignaValor(int, T);
void Lectura();
void Escribe();
};

template <class T>
Arreglo<T>::Arreglo()
{
Tam= 0;
}

template <class T>
int Arreglo<T>::RegresaTam()
{
return Tam;
}

template <class T>
T Arreglo<T>::RegresaValor(int Indice)
{
return Datos[Indice];
}

template <class T>
void Arreglo<T>::AsignaValor(int Indice, T Valor)
{
Datos[Indice]= Valor;
}

template <class T>
void Arreglo<T>::Lectura()
{
int Indice;

do {
cout<<"\n\n Ingrese total de elementos: ";
cin>> Tam;
} while (Tam < 1 || Tam > MAX);

for (Indice= 0; Indice < Tam; Indice++)
{
cout<<"\nIngrese el "<<Indice + 1<<" dato: ";
cin>> Datos[Indice];
}
}

template <class T>
void Arreglo<T>::Escribe()
{
int Indice;
if (Tam > 0)
{
cout<<"\n\n";
for (Indice= 0; Indice < Tam; Indice++)
cout<< "\t" << Datos[Indice];
cout<<"\n\n";
}
else
cout<< "\n No hay elementos almacenados.";
}

MetOrdena.h



#define MAX 100 
template <class T>
class Arreglos
{
//private:
public:
T Datos[MAX];
int Tam;

Arreglos();
int RegresaTam();
T RegresaValor(int);
void AsignaValor(int, T);
void Intercambia(int, int);
void IntercDirectoIzq();
void InsercionDirecta();
void SeleccionDirecta();
void QuickSort();
void Reduce(int, int);
void Lectura();
void Escribe();
};

template <class T>
Arreglos<T>::Arreglos()
{
Tam=0;
}

template <class T>
void Arreglos<T>::Intercambia(int Ind1, int Ind2)
{
T Auxiliar;
Auxiliar= Datos[Ind1];
Datos[Ind1]= Datos[Ind2];
Datos[Ind2]= Auxiliar;
}

template <class T>
void Arreglos<T>::IntercDirectoIzq()
{
int Ind1, Ind2;
for (Ind1= 1; Ind1< Tam; Ind1++)
for (Ind2= Tam-1; Ind2 >= Ind1; Ind2--)
if (Datos[Ind2-1] > Datos[Ind2])
Intercambia(Ind2-1, Ind2);
}

template <class T>
void Arreglos<T>::InsercionDirecta()
{
int Auxiliar, Indice, IndAux;
for (Indice= 1; Indice < Tam; Indice++)
{
Auxiliar= Datos[Indice];
IndAux= Indice - 1;
while ((IndAux >= 0) && (Auxiliar < Datos[IndAux]))
{
Datos[IndAux+1]= Datos[IndAux];
IndAux--;
}
Datos[IndAux+1]= Auxiliar;
}
}

template <class T>
void Arreglos<T>::SeleccionDirecta()
{
int Menor, Ind1, Ind2, Ind3;
for (Ind1= 0; Ind1 < Tam-1; Ind1++)
{
Menor= Datos[Ind1];
Ind2= Ind1;
for (Ind3= Ind1+1; Ind3 < Tam; Ind3++)
if (Datos[Ind3] < Menor)
{
Menor= Datos[Ind3];
Ind2= Ind3;
}
Datos[Ind2]= Datos[Ind1];
Datos[Ind1]= Menor;
}
}

template <class T>
void Arreglos<T>::QuickSort()
{
Reduce(0, Tam-1);
}

template <class T>
void Arreglos<T>::Reduce(int Inicio, int Fin)
{
if ( Tam > 0)
{
int Izq, Der, Posic, Bandera;
Izq= Inicio;
Der= Fin;
Posic= Inicio;
Bandera= 1;
while (Bandera)
{
Bandera= 0;
while ((Datos[Posic] <= Datos[Der]) && (Posic != Der))
Der--;
if (Posic != Der)
{
Intercambia(Posic, Der);
Posic= Der;
while ((Datos[Posic] >= Datos[Izq]) && (Posic != Izq))
Izq++;
if (Posic != Izq)
{
Bandera=1;
Intercambia(Posic, Izq);
Posic= Izq;
}
}
}
if ((Posic-1) > Inicio)
Reduce(Inicio, Posic-1);
if (Fin > (Posic+1))
Reduce(Posic+1, Fin);
}
}
  • 1
    Add header guards in the `.h` files – Ted Lyngmo Feb 26 '22 at 03:28
  • 1
    You'll also want to move the definitions of non-member functions from header to cpp file. Things like `operator>>` and `operator<<`. – Retired Ninja Feb 26 '22 at 03:30
  • On the `operator<<` topic. The object you stream out should be taken by `const&` since you don't aim to change it. – Ted Lyngmo Feb 26 '22 at 03:35
  • 2
    And of course, [stop polluting headers with `using namespace std;`](https://www.geeksforgeeks.org/using-namespace-std-considered-bad-practice/). Regardless, see this: [Purpose of Header Guards](https://stackoverflow.com/questions/2979384/purpose-of-header-guards). Still blows my mind how many lines of code someone can write without ever-once tapping the compile button on their UI to see if they're still on the rails. – WhozCraig Feb 26 '22 at 03:43
  • Also, `IntercDirectoIzq Orden;` doesn't make sense. `IntercDirectoIzq` is a member function in `Arreglos`, not a type. And the member function `Ordena` that you call in `Orden.Ordena(&Escuela);` isn't declared or defined anywhere. – Ted Lyngmo Feb 26 '22 at 03:43
  • There are too may mistakes in the given program. For example, `IntercDirectoIzq` is a member function so `IntercDirectoIzq Orden;` doesn't make sense. A [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) is recommended. – Jason Feb 26 '22 at 03:47
  • 1
    Some advice: Don't wait until you have this much code before you compile it for the first time. Make one addition at a time, then compile and fix the errors. Then you go on to the next part etc. That way there will be just very few problems to deal with instead of this large amount. – Ted Lyngmo Feb 26 '22 at 03:49
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Abhishek Dutt Feb 26 '22 at 09:34

1 Answers1

0

You should always add header guards in the header files. Additionally, you have defined the member functions for class Alumno inside the header. Better would be to put the implementation of member functions of non-template class like Alumno in source files as shown below (See DEMO). The changes i made are listed at the end of my answer.

Alumno.h

#ifndef ALUMNO_H //add include guards
#define ALUMNO_H
#include <iostream>
//removed using namespace std;

class Alumno {
    private:
    int Clave;
    char Nombre[64];
    public:
    Alumno();
    Alumno(int, char *);
    int operator > (Alumno);
    friend std::istream &operator >> (std::istream &, Alumno &);
    friend std::ostream &operator << (std::ostream &,const Alumno &); //made the second parameter a reference to const Alumno
};

#endif

Alumno.cpp

#include "Alumno.h"
#include<stdio.h>
#include<cstring>

Alumno::Alumno()
{}

Alumno::Alumno(int Cla, char Nom[])
{
Clave= Cla;
strcpy(Nombre, Nom);
}

int Alumno::operator > (Alumno ObjAl)
{
    if (Clave > ObjAl.Clave)
    return 1;
    else
    return 0;
}

std::istream &operator>>(std::istream &Lee,Alumno &ObjAl)
{
    std::cout<<"\n\nIngrese clave del alumno: ";
    Lee>>ObjAl.Clave;
    std::cout<<"\n\nIngrese nombre del alumno: ";
    Lee>>ObjAl.Nombre;
    return Lee;
}

std::ostream &operator << (std::ostream &Escribe,const  Alumno &ObjAl)//made the second parameter a reference to const Alumno
{
Escribe<<"\n\nDatos del alumno\n";
Escribe<<"\nClave: "<<ObjAl.Clave;
Escribe<<"\nNombre: "<<ObjAl.Nombre<<"\n";
return Escribe;
}

MetOrdena.h

#ifndef METORDENA_H
#define METORDENA_H
#define MAX 100 
template <class T>
class Arreglos
{
    //private:
    public:
        T Datos[MAX];
        int Tam;
        
        Arreglos();
        int RegresaTam();
        T RegresaValor(int);
        void AsignaValor(int, T);
        void Intercambia(int, int);
        void IntercDirectoIzq();
        void InsercionDirecta();
        void SeleccionDirecta();
        void QuickSort();
        void Reduce(int, int);
        void Lectura();
        void Escribe();
};

template <class T>
Arreglos<T>::Arreglos()
{
Tam=0;
}

template <class T>
void Arreglos<T>::Intercambia(int Ind1, int Ind2)
{
T Auxiliar;
Auxiliar= Datos[Ind1];
Datos[Ind1]= Datos[Ind2];
Datos[Ind2]= Auxiliar;
}

template <class T>
void Arreglos<T>::IntercDirectoIzq()
{
int Ind1, Ind2;
for (Ind1= 1; Ind1< Tam; Ind1++)
for (Ind2= Tam-1; Ind2 >= Ind1; Ind2--)
if (Datos[Ind2-1] > Datos[Ind2])
Intercambia(Ind2-1, Ind2);
}

template <class T>
void Arreglos<T>::InsercionDirecta()
{
int Auxiliar, Indice, IndAux;
for (Indice= 1; Indice < Tam; Indice++)
{
Auxiliar= Datos[Indice];
IndAux= Indice - 1;
while ((IndAux >= 0) && (Auxiliar < Datos[IndAux]))
{
Datos[IndAux+1]= Datos[IndAux];
IndAux--;
}
Datos[IndAux+1]= Auxiliar;
}
}

template <class T>
void Arreglos<T>::SeleccionDirecta()
{
int Menor, Ind1, Ind2, Ind3;
for (Ind1= 0; Ind1 < Tam-1; Ind1++)
{
Menor= Datos[Ind1];
Ind2= Ind1;
for (Ind3= Ind1+1; Ind3 < Tam; Ind3++)
if (Datos[Ind3] < Menor)
{
Menor= Datos[Ind3];
Ind2= Ind3;
}
Datos[Ind2]= Datos[Ind1];
Datos[Ind1]= Menor;
}
}

template <class T>
void Arreglos<T>::QuickSort()
{
Reduce(0, Tam-1);
}

template <class T>
void Arreglos<T>::Reduce(int Inicio, int Fin)
{
if ( Tam > 0)
{
int Izq, Der, Posic, Bandera;
Izq= Inicio;
Der= Fin;
Posic= Inicio;
Bandera= 1;
while (Bandera)
{
Bandera= 0;
while ((Datos[Posic] <= Datos[Der]) && (Posic != Der))
Der--;
if (Posic != Der)
{
Intercambia(Posic, Der);
Posic= Der;
while ((Datos[Posic] >= Datos[Izq]) && (Posic != Izq))
Izq++;
if (Posic != Izq)
{
Bandera=1;
Intercambia(Posic, Izq);
Posic= Izq;
}
}
}
if ((Posic-1) > Inicio)
Reduce(Inicio, Posic-1);
if (Fin > (Posic+1))
Reduce(Posic+1, Fin);
}
}

#endif

Arreglo.h

#ifndef ARREGLO_H
#define ARREGLO_H
#include<iostream>
#include<stdio.h>

#define MAX 100

using namespace std;

template <class T>
class Arreglo
{
    private:
        T Datos[MAX];
        int Tam;
    public:
        Arreglo();
        int RegresaTam();
        T RegresaValor(int);
        void AsignaValor(int, T);
        void Lectura();
        void Escribe();
};

template <class T>
Arreglo<T>::Arreglo()
{
Tam= 0;
}

template <class T>
int Arreglo<T>::RegresaTam()
{
return Tam;
}

template <class T>
T Arreglo<T>::RegresaValor(int Indice)
{
return Datos[Indice];
}

template <class T>
void Arreglo<T>::AsignaValor(int Indice, T Valor)
{
Datos[Indice]= Valor;
}

template <class T>
void Arreglo<T>::Lectura()
{
int Indice;

do {
cout<<"\n\n Ingrese total de elementos: ";
cin>> Tam;
} while (Tam < 1 || Tam > MAX);

for (Indice= 0; Indice < Tam; Indice++)
{
cout<<"\nIngrese el "<<Indice + 1<<" dato: ";
cin>> Datos[Indice];
}
}

template <class T>
void Arreglo<T>::Escribe()
{
int Indice;
if (Tam > 0)
{
cout<<"\n\n";
for (Indice= 0; Indice < Tam; Indice++)
cout<< "\t" << Datos[Indice];
cout<<"\n\n";
}
else
cout<< "\n No hay elementos almacenados.";
}
#endif

main.cpp

#include "Alumno.h"
#include "Arreglo.h"
#include "MetOrdena.h"
#include<iostream>

using namespace std;

int main()
{

Arreglo<Alumno> Escuela;
Escuela.Lectura();
/*
IntercDirectoIzq<Alumno> Orden;
Orden.Ordena(&Escuela);
*/
Escuela.Escribe();

if (Escuela.RegresaTam() != 0)
cout<<"Los datos del primer alumno son:"<<endl;
cout<< Escuela.RegresaValor(0);
return 0;
}

The output of the above modified program can be seen here.

As you can see, i have refactored the code. Some of the changes that i made include:

  1. Added header guards inside headers. This is a recommended practice.
  2. Moved the implementation of member functions of non-template class Alumno to a separate source file called Alumno.cpp.
  3. Added low-level const to the second parameter of friend function declaration of overloaded operator<< inside Alumno.h and Alumno.cpp.
  4. Added a main.cpp file where main() function is defined.
  5. Removed IntercDirectoIzq<Alumno> Orden; since IntercDIrectoIzq is a member function and so the removed statement doesn't make sense.
Jason
  • 36,170
  • 5
  • 26
  • 60