Why do I get an “Undefined reference error” when implementing a singleton class? [duplicate]

99封情书 提交于 2019-12-24 01:27:32

问题


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:


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;



回答2:


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();



回答3:


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");


来源:https://stackoverflow.com/questions/36582591/why-do-i-get-an-undefined-reference-error-when-implementing-a-singleton-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!