I'am trying to implement a method that returns me an instance of the class, but it's is crashing when it tries to create the instance at the first time. I don't know how to implement the singleton in C++/QT
Main
#include <QCoreApplication>
#include "carro.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Carta* nueva;
nueva->getInstance();
nueva->setColor("rojo");
cout << nueva->getColor() << endl;
return a.exec();
}
Carro.h
#ifndef CARRO_H
#define CARRO_H
#include <string>
#include <iostream>
using namespace std;
class Carta{
private:
string cara; //valor
string palo; //simbolo
string color;
string direccion;
static Carta* m_instance;
public:
//constructor
Carta(){
}
static Carta* getInstance(){
if(!m_instance){
m_instance = new Carta;
}
return m_instance;
}
string getDireccion(){
return direccion;
}
void setColor(string pcolor){
color = pcolor;
}
string getColor(){
return this->color;
}
string getPalo(){
return this->palo;
}
string getCara(){
return this->cara;
}
//print
string print(){
return (cara + " de " + palo + " Color: "+color);
}
};
#endif // CARRO_H