When trying to make an arrangement of objects, I get the error array must be initialized

0

When trying to make an arrangement of Students objects to create a file in C ++, I get the following error:

 ../Expediente.h:18:36: error: array must be initialized with a brace-enclosed initializer

This is the code I have:

class Expediente{
        public:
            int contadorAlumno = 0, tam = 5;
            /*Alumno *ptrAlumno;
            ptrAlumno = new Alumno[tam];*/
            Alumno arrgAlumno[] = new Alumno[5];
            Expediente();
            void agregarAlumno();
            void mostrarExpediente();
            virtual ~Expediente();
        };
        Expediente::Expediente() {
            int opc = 0;
            do{
                cout<<"MENU EXPEDIENTE"<<endl;
                cout<<"1.Agregar Alumno"<<endl;
                cout<<"2.Mostrar expediente de Alumno"<<endl;
                cout<<"¿Que opcion desea: ";
                cin>>opc;
                switch(opc){
                case 1:
                    /*(ptrAlumno+contadorAlumno)->agregarAlumno();
                    contadorAlumno++;*/
                    agregarAlumno();
                    break;
                case 2:
                    mostrarExpediente();
                    /*cout<<"Listado de Alumnos"<<endl;
                        for(int i=0; i<contadorAlumno; i++){
                            cout<< "Nombre del Alumno: "<< getNombre()<<endl;
                            cout<<"Numero de Cuenta: "<<getNumeroCuenta()<<endl;
                            cout<<"Edad del Alumno: "<<getEdad()<<endl;
                            cout<<"Nota final del Alumno: "<<getNotaFinal()<<endl;
                        }

                     */
                    break;

                default:
                    cout<<"No es una opcion valida"<<endl;
                }
            }while(opc!=3);


        }

        void Expediente::agregarAlumno(){
            string nombreA, numeroCuentaA;
            int edadA;
            float notaFinalA;

            cout<<"Nombre del Alumno: ";
            cin>>nombreA;
            cout<<"Numero de cuenta: ";
            cin>>numeroCuentaA;
            cout<<"Edad: ";
            cin>>edadA;
            cout<<"Nota Final: ";
            cin>>notaFinalA;

            Alumno alumnoTemp = new Alumno();
            alumnoTemp.setNombre(nombreA);
            alumnoTemp.setEdad(edadA);
            alumnoTemp.setNumeroCuenta(numeroCuentaA);
            alumnoTemp.setNotaFinal(notaFinalA);

            arrgAlumno[contadorAlumno] = alumnoTemp;
            contadorAlumno++;

        }
    
asked by EmSanchez 17.03.2018 в 17:24
source

1 answer

0

First, if you use dynamic memory, I would recommend that you define / declare a destructor, second, new return a pointer to the reserved memory according to the size of your selected class ... Then a quick little example:

class Alumno {};

class Expediente
{
    public:         
        Alumno* cAlumno;
        Expediente();   
        ~Expediente();  

};

Expediente::Expediente() : cAlumno(new Alumno[5]) // sz = 5 * sizeof(Alumno)
{
    // [...]
}

Expediente::~Expediente()
{
    delete[] cAlumno;
}
    
answered by 17.03.2018 / 22:52
source