2

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
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
nanokuro
  • 63
  • 2
  • 9

3 Answers3

7

You've been missing to define static Carta* m_instance;, hence the linker error:

    Carta* Carta::m_instance = nullptr;

However I'd rather recommend this pattern, if you're really sure you need a singleton:

static Carta* getInstance() {
    static Carta m_instance;
    return &m_instance;
}

or

static Carta& getInstance() {
    static Carta m_instance;
    return m_instance;
}

Also for accessing the singleton you should have some code like

Carta* nueva = Carta::getInstance();
nueva->setColor("rojo");
cout << nueva->getColor() << endl;

or with the reference vesion

Carta& nueva = Carta::getInstance();
nueva.setColor("rojo");
cout << nueva.getColor() << endl;
Community
  • 1
  • 1
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
1

These two lines are incorrect:

Carta* nueva;
nueva->getInstance();

nueva isn't initialized to anything, so deferencing it in the next line results in undefined behavior.

What it looks like you're intending to do is the following:

Carta* nueva = Carta::getInstance();
Open Kastle
  • 427
  • 3
  • 13
0
Carta* nueva;
nueva->getInstance();
nueva->setColor("rojo");

This is incorrect.These lines of codes exactly say to the computer :

1.Carta* nueva Make a pointer to a Carta variable name nueva. Don't forget that at this point, the pointer is NULL.

2.nueva->getInstance() This mean access the nueva pointer ( that is still null) and get the method getInstance(). Since getInstance is a static function, it should be use in this way Carta::getInstance(); You have to use the class instead of a variable and the operator :: to access static members. After you put it in your variable nueva so it would give this : nueva = Carta::getInstance();

So the correct writing would be :

Carta* nueva;
nueva = Carta::getInstance();
nueva->setColor("rojo");
Matriac
  • 382
  • 3
  • 11