Function does not add to list

Asked

Viewed 66 times

1

THIS IS THE SOURCE:

#include <iostream>
#include "gestor.h"
using namespace std;


int menu()
{ 
int opc;


    cout << "Faca a sua escolha!" << endl;
    cout << "1-> inserir avaria!" << endl;
    cout << "2-> listar avaria!" << endl;
    cout << "0-> sair" << endl;
    cin >> opc;

    return opc;


    }
    gestor *criargestor()
    {
    gestor*g;
    g = new gestor();
   return g;

}

void destruirgestor(gestor*g)
{
delete g;
}


void main()
{
   gestor *g;
   g = criargestor();
int opcao;

do
{   
    opcao = menu();

    switch (opcao)
    {
    case 1: g->addavaria();
        break;
    case 2:g->listaravarias();
        break;
    case 0:
        break;
    default:
        cout << "invalido!\n" << endl;
    }
} while (opcao!= 0);
destruirgestor(g);
}

THIS AND THE FILE HEADER MALFUNCTIONS. h

#pragma once
#include<string>
#include<iostream>
#include<locale.h>
using namespace std;
class avaria
{
int codigoavaria;
string nome;
string descricao;
public:
avaria(void);
avaria(int cod,string nm,string des);
void mostrar();
~avaria();
};

THIS IS THE BREAKDOWN.CPP

#include "avaria.h"

 avaria::avaria(void)// construtor da função vai inicializar as variáveis.
 {
cout << " passei na função= [" << __FUNCTION__ << "]" << endl;
}
avaria::avaria(int cod, string nm,string des) 
{
setlocale(LC_ALL, "portuguese");
cout << " passei na função= [" << __FUNCTION__ << "]" << endl;
codigoavaria = cod;
nome =nm;
descricao = des;

}
void avaria::mostrar()
{
setlocale(LC_ALL, "portuguese");
cout << " passei na função= [" << __FUNCTION__ << "]" << endl;
cout << " codigo =" << codigoavaria << endl;
cout << "nome =" << "[" << nome << "]" << endl;
cout << "descrição" << "[" << descricao << "]" << endl;

}

avaria::~avaria()
{
setlocale(LC_ALL, "portuguese");
cout << " passei na função= [" << __FUNCTION__ << "]" << endl;
}

THIS IS THE MANAGER. H

#pragma Once #include #include #include"malfunction. h" management class { list Lav;

public:
gestor();
void addavaria();
void listaravarias();
~gestor();
};

THIS IS THE CPP MANAGER

#include "gestor.h"


gestor::gestor()
{

}

 void gestor::addavaria()
{
 setlocale(LC_ALL, "portuguese");


string descricao;
string nome;
int tipo;
avaria*av = NULL;

cout << "Introduza o tipo de avaria! (1) electrica (2) mecanica" <<         endl;
    cin >> tipo;
    cin.ignore();
    cout << " Introduza o nome da avaria!" << endl;
    getline(cin, nome);
    cout << "Introduza a descrição da avaria!" << endl;
    getline(cin,descricao);
    av = new avaria (tipo,nome,descricao);


}
 void gestor::listaravarias()
{
cout << "N. de Elementos da Lista = " << lav.size() << endl;
for (list<avaria*>::iterator Iter = lav.begin();
    Iter != lav.end(); Iter++)
{
    cout << "................................." << endl;
    cout << endl;
    (*Iter)->mostrar();
    cout << endl;
    cout << "..................................." << endl;
}
}


gestor::~gestor()
{
}

What happens is the following function addavaria() does not seem to be adding any malfunction to the list, every time I compile add a malfunction and then I show and the function says that there are zero elements added to the list. the program is in c++ and is working with a class manager (no hierarchy) whose function is to manage all failures of the class breakdown.

  • if you need some images that are not visible tell me

  • Do not place images with code, put the code.

  • I can email you my program?

  • No, put the code you can see the error and nothing else. If this is not possible, the question cannot be asked here.

  • and enough code and I can’t organize the question, with separate . hs cpps

  • http://answall.com/help/mcve

  • editions were made ;)

  • From what I understand Voce creates the broken object and does not add it in the list of the manager class. Just add it in the list.

  • How do I do that?

Show 4 more comments

1 answer

0

In gestor::addavaria(), av is not being added to the list, ie, is missing a lav.push_back(av).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.