gpt4 book ai didi

c++ - 为什么在实现单例类时得到 "Undefined reference error"?

转载 作者:太空狗 更新时间:2023-10-29 23:49:49 24 4
gpt4 key购买 nike

<分区>

我正在尝试实现一个返回该类实例的方法,但它在第一次尝试创建实例时崩溃了。我不知道如何在C++/QT中实现单例

主要

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

卡罗.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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com