Solo puse el titulo del patron que estoy usando, pero ese no es problema(quizas se a mi parecer un problema de header) igualmente trate de detallar todo en mi codigo con sus respectivo error de compilacion. Si alguien tiene alguna idea de solucionarlo le agradeceria por la ayuda.
#ifndef SUPERMARKET_SUPERMARKET_H_
#define SUPERMARKET_SUPERMARKET_H_
#include "CountryFactory.h"
class Supermarket {
private:
Menu *menu;
Ticket *ticket;
public:
Supermarket();
void vender();
virtual ~Supermarket();
};
#endif
#include "Supermarket.h"
Supermarket::Supermarket() {
ticket = CountryFactory::getInstance()->createTicket();
menu = CountryFactory::getInstance()->createMenu();
}
void Supermarket::vender() {
bool terminado = false;
do {
menu->execute(ticket);
}while (!terminado);
}
Supermarket::~Supermarket() {
delete ticket;
delete menu;
}
Aplicando abstract Factory con singleton
#ifndef SUPERMARKET_COUNTRYFACTORY_H_
#define SUPERMARKET_COUNTRYFACTORY_H_
#include "Menu.h"
#include "Ticket.h"
class CountryFactory {
protected:
CountryFactory();
static CountryFactory* instance;
public:
static CountryFactory* getInstance();
virtual Menu* createMenu();
virtual Ticket* createTicket();
virtual ~CountryFactory();
};
#endif
Aqui no se como eliminar un singleton con el destructor sin que me de un bucle infinito
#include "CountryFactory.h"
#include "SpanishFactory.h"
CountryFactory* CountryFactory::instance = nullptr;
CountryFactory::CountryFactory() {
}
CountryFactory* CountryFactory::getInstance() {
if(instance){
instance = new SpanishFactory();
}
return instance;
}
CountryFactory::~CountryFactory() {
}
Aqui es donde me salta el problema, que creo que un tema de header.
#ifndef SUPERMARKET_COUNTRYFACTORY_H_
#define SUPERMARKET_COUNTRYFACTORY_H_
#include "CountryFactory.h"
class SpanishFactory : public CountryFactory{
public:
explicit SpanishFactory();
Menu* createMenu() override;
Ticket* createTicket() override;
virtual ~SpanishFactory();
};
#endif
member declaration not found(en cada uno)
SpanishFactory::SpanishFactory() : CountryFactory() {
}
Menu* SpanishFactory::createMenu() {
return new Menu();
}
Ticket* SpanishFactory::createTicket() {
return new Ticket();
}
SpanishFactory::~SpanishFactory() {
}
Tipo de error al compilar:
20:49:16 **** Incremental Build of configuration Debug for project supermarket ****
make all
Building file: ../src/supermarket/CountryFactory.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/supermarket/CountryFactory.d" -MT"src/supermarket/CountryFactory.o" -o "src/supermarket/CountryFactory.o" "../src/supermarket/CountryFactory.cpp"
../src/supermarket/CountryFactory.cpp: In static member function ‘static CountryFactory* CountryFactory::getInstance()’:
../src/supermarket/CountryFactory.cpp:12:18: error: expected type-specifier before ‘SpanishFactory’
instance = new SpanishFactory();
^~~~~~~~~~~~~~
make: *** [src/supermarket/CountryFactory.o] Error 1
src/supermarket/subdir.mk:33: recipe for target 'src/supermarket/CountryFactory.o' failed
20:49:17 Build Finished (took 637ms)
#define SUPERMARKET_COUNTRYFACTORY_H_
?