0
Code of the program
main
#include <iostream>
#include "CntrIU.h"
#include "CntrNeg.h"
#include "interfaces.h"
#include "stubs.h"
#include <cstdlib>
using namespace std;
int main()
{
IUAutenticacao *CntrAut = new CntrIUAut();
ILNAutenticacao *CntrLNA = new CntrLNAutenticar();
ResultAut resulted;
CntrAut->setCntrLNAutenticacao(CntrLNA);
try{
resulted = CntrAut->autenticar();
}catch(const runtime_error &e){
cout << "Erro falha no sistema" << endl;
}
if(resulted.getValor()==ResultAut::sucesso){
cout << "Autenticacao feita com sucesso" << endl;
}
return 0;
}
Stubs. h
#ifndef STUBS_H
#define STUBS_H
#include "Entidades.h"
#include "Dominios.h"
#include "Interfaces.h"
#include "Comandos.h"
#include <stdexcept>
#include <iostream>
#include <windows.h>
#include <typeinfo>
class StubIPersistencia:public IPersistencia
{
private:
TRIGGERS t;
public:
void executar(CmdBD*)throw(runtime_error);
};
#endif // STUBS_H
Stubs.cpp
void StubIPersistencia::executar(CmdBD*cmd)throw(runtime_error)
{
try{
if(typeid(*cmd) == typeid(CmdRecuperarSenha)){
cmd->executar();
}
if(typeid(*cmd) == typeid(CmdIncluirGerProj)){
cmd->executar();
}
if(typeid(*cmd) == typeid(CmdEditarGerProj)){
cmd->executar();
}
if(typeid(*cmd) == typeid(CmdRecuperarGerProj)){
cmd->executar();
}
if(typeid(*cmd) == typeid(CmdRemoverGerProj)){
cmd->executar();
}
if(typeid(*cmd) == typeid(CmdIncluirDesenv)){
cmd->executar();
}
if(typeid(*cmd) == typeid(CmdEditarDesenv)){
cmd->executar();
}
if(typeid(*cmd) == typeid(CmdRecuperarDesenv)){
cmd->executar();
}
if(typeid(*cmd) == typeid(CmdRemoverDesenv)){
cmd->executar();
}
if(typeid(*cmd) == typeid(CmdIncluirProj)){
cmd->executar();
}
if(typeid(*cmd) == typeid(CmdEditarProj)){
cmd->executar();
}
if(typeid(*cmd) == typeid(CmdRecuperarProj)){
cmd->executar();
}
if(typeid(*cmd) == typeid(CmdRemoverProj)){
cmd->executar();
}
}catch(const runtime_error&e){
throw runtime_error(e);
}
}
Commands. h
#ifndef COMANDOS_H
#define COMANDOS_H
#include "Entidades.h"
class CmdBD
{
public:
virtual void executar()throw(runtime_error) = 0;
virtual ~CmdBD(){};
};
class CmdRecuperarSenha:public CmdBD
{
private:
matricula m;
public:
void executar()throw(runtime_error);
CmdRecuperarSenha(matricula x);
};
#endif // COMANDOS_H
Commands.cpp.
CmdRecuperarSenha::CmdRecuperarSenha(matricula x)
{
this->m = x;
}
void CmdRecuperarSenha::executar()throw(runtime_error)
{
if(m.get()==TRIGGERSP::TRIGGERSUCESSOMAT){
return;
}else if(m.get()==TRIGGERSP::TRIGGERFALHAMAT){
throw runtime_error("Erro no Banco de Dados Ao Recuperar Senha");
}
}
Interfaces. h
#ifndef INTERFACES_H
#define INTERFACES_H
#include "dominios.h"
#include "entidades.h"
#include "Comandos.h"
#include <stdexcept>
using namespace std;
class IUAutenticacao{
public:
virtual ResultAut autenticar() throw(runtime_error) = 0;
virtual void setCntrLNAutenticacao(ILNAutenticacao *) = 0;
};
class ILNAutenticacao{
public:
virtual ResultAut autenticar(const matricula&,const senha&) throw(runtime_error) = 0;
};
class IPersistencia{
public:
virtual void executar(CmdBD*)throw(runtime_error) = 0;
};
#endif // INTERFACES_H
Cntriu. h
#ifndef CNTRIU_H
#define CNTRIU_H
#include "interfaces.h"
#include "stubs.h"
#include "Comandos.h"
#include <stdexcept>
#include <cstdlib>
#include <iostream>
#include <cstdio>
using namespace std;
class CntrIUAut:public IUAutenticacao
{
private:
ILNAutenticacao *cntrLNAutenticacao;
public:
ResultAut autenticar() throw(runtime_error);
void setCntrLNAutenticacao(ILNAutenticacao *cntrLNAutenticacao);
};
Cntriu.cpp
#include "CntrIU.h"
ResultAut CntrIUAut::autenticar() throw(runtime_error)
{
ResultAut result;
matricula m;
senha s;
string n;
bool t = true;
while(t == true){
system("cls");
cout << "Autenticacao Usuario" << endl;
try{
cout << "Digite sua Matricula: "<< endl;
cin >> n;
m.Set(n);
cout << "Digite sua Senha: "<< endl;
cin >> n;
s.Set(n);
t = false;
}catch(const invalid_argument &e){
system("cls");
cout << "Dados Inseridos INVALIDOS" << endl;
getchar();
}
}
result = cntrLNAutenticacao->autenticar(m,s);
if(result.getValor() == ResultAut::falha){
cout << "Falha na autenticacao" << endl;
}
return result;
}
void CntrIUAut::setCntrLNAutenticacao(ILNAutenticacao *cntrLNAutenticacao)
{
this->cntrLNAutenticacao = cntrLNAutenticacao;
}
Cntrneg. h
#ifndef CNTRNEG_H
#define CNTRNEG_H
#include "Interfaces.h"
#include "stubs.h"
#include "Comandos.h"
#include <iostream>
#include <stdexcept>
#include <windows.h>
using namespace std;
class CntrLNAutenticar:public ILNAutenticacao {
private:
IPersistencia * CntrPersistencia;
public:
void setCntrPersistencia(IPersistencia * CntrPersistencia);
ResultAut autenticar(const matricula&,const senha&)throw(runtime_error); };
#endif // CNTRNEG_H
Cntrneg.cpp
ResultAut CntrLNAutenticar::autenticar(const matricula&m,const senha&s)throw(runtime_error)
{
ResultAut result;
CntrPersistencia = new StubIPersistencia();
CmdBD *cmd = new CmdRecuperarSenha(m);
cout << "Autenticando..." << endl;
cout << "matricula:" << m.get() << endl;
cout << "senha:" << s.get() << endl;
Sleep(1000);
try{
CntrPersistencia->executar(cmd);
result.setValor(ResultAut::sucesso);
}catch(const runtime_error &e){
cout << e << endl;
result.setValor(ResultAut::falha);
}
return result;
}
Well I’m having that problem, more precisely in this part of my code
try{
CntrPersistencia->executar(cmd);
result.setValor(ResultAut::sucesso);
}catch(const runtime_error &e){
cout << e << endl;
result.setValor(ResultAut::falha);
}
I’m not going to post all the code, because it’s a giant code, the problem is in the print that I try to give in error and constant that I declared in catch
error: no match for 'Operator<<' (operand types are 'Std::ostream {aka Std::basic_ostream}' and 'const Std::runtime_error')
I wanted to print that constant for the user, someone knows a way to solve that problem? thanks
Only with this passage can not answer, we do not know what it is
e
.– Maniero
e would be an error, as specified in the catch, would be an error message in the function Cntrpersistencia->execute(cmd),but I will try to put a larger portion of the code, for those who have the patience to read
– ReZ
It is I tried,not to put the error code when I put it,says here that I messed up the limit of 3000 characters of the code body with 6628,but basically it was what I said,o 'and' is a function error that is caught,the statement basically and throw runtime_error(string),what I wanted to do and just print this string on the screen
– ReZ
But it is not to put all the code, it is to put only what we need so we can help. See how to make a [mcve].
– Maniero
okay, I’m gonna try to put just the piece of code that’s executed,
– ReZ
blz posted a few more excerpts of the code that runs,
– ReZ
You tried to trade the line for
cout << e.what() << endl;
? The error does not seem to have to do with the constant, but rather with the impression of the object of the exception.– Luiz Vieira
Putz vlw bro, was that right,I always forget that what function
– ReZ