シングルトンパターンでリンクエラー

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
dic
記事: 658
登録日時: 14年前
住所: 宮崎県
連絡を取る:

シングルトンパターンでリンクエラー

#1

投稿記事 by dic » 6年前

つまづきました。
シングルトンパターンで作っているのですが、コンパイルは通るのですが、
リンクができません。
どこがまずいのでしょうか?
以下がソースです。

[CPlayer.h]

コード:

#pragma once

class CPlayer
{
	double m_x;
	double m_y;
	double m_hp;

	static CPlayer* _instance;
public:
	static CPlayer* GetInstance();
	CPlayer();
	~CPlayer();
	void Init();
	void Draw();
	void Move();
};

[CPlayer.cpp]

コード:

#include "stdafx.h"


CPlayer::CPlayer()
{
	_instance = NULL;
}
CPlayer::~CPlayer()
{
}
CPlayer* CPlayer::GetInstance()
{
	if (_instance == NULL)
		_instance = new CPlayer();
	return _instance;
}
void CPlayer::Init()
{
	m_x = 320;
	m_y = 480;
	m_hp = 1000;
}
void CPlayer::Draw()
{
	DrawBox((int)m_x - 8, (int)m_y - 8, (int)m_x + 8, (int)m_y + 8, GetColor(0, 255, 0), true);
}
void CPlayer::Move()
{
}
[CSystemData.h]

コード:

#pragma once
#include "stdafx.h"


#define	INIT_FILE	"setting.txt"
int dic_Init();


#define FLAME 30
void fps_wait();
void draw_fps(int x, int y);


class CSystemData
{
	static CSystemData* _instance;
public:
	static CSystemData* GetInstance();
	CSystemData();
	~CSystemData();
	void Init();
};

[CSystem.cpp]

コード:


CSystemData::CSystemData()
{
}
CSystemData::~CSystemData()
{
}
CSystemData* CSystemData::GetInstance()
{
	if (_instance == NULL)
		_instance = new CSystemData();
	return _instance;
}
void CSystemData::Init()
{
}
エラー内容です。
重大度レベル コード 説明 プロジェクト ファイル 行 抑制状態
エラー LNK1120 2 件の未解決の外部参照 DxLib_2 C:\Users\user\Documents\Visual Studio 2017\source\DxLib_2\Debug\DxLib_2.exe 1

dic
記事: 658
登録日時: 14年前
住所: 宮崎県
連絡を取る:

Re: シングルトンパターンでリンクエラー

#2

投稿記事 by dic » 6年前

操作ミス
エラーの続きです。

重大度レベル コード 説明 プロジェクト ファイル 行 抑制状態
エラー LNK2001 外部シンボル ""private: static class CPlayer * CPlayer::_instance" (?_instance@CPlayer@@0PAV1@A)" は未解決です。 DxLib_2 C:\Users\user\Documents\Visual Studio 2017\source\DxLib_2\DxLib_2\CPlayer.obj 1

重大度レベル コード 説明 プロジェクト ファイル 行 抑制状態
エラー LNK2001 外部シンボル ""private: static class CSystemData * CSystemData::_instance" (?_instance@CSystemData@@0PAV1@A)" は未解決です。 DxLib_2 C:\Users\user\Documents\Visual Studio 2017\source\DxLib_2\DxLib_2\CSystem.obj 1

dic
記事: 658
登録日時: 14年前
住所: 宮崎県
連絡を取る:

Re: シングルトンパターンでリンクエラー

#3

投稿記事 by dic » 6年前

自己解決しました。

[CPlayer.cpp]に
CPlayer* CPlayey::_instance = 0;

[CSystemData.cpp]に
CSystemData* CSystemData::_instance = 0;

を追加しました。

アバター
あたっしゅ
記事: 667
登録日時: 14年前
住所: 東京23区
連絡を取る:

Re: シングルトンパターンでリンクエラー

#4

投稿記事 by あたっしゅ » 6年前

https://qiita.com/kikuuuty/items/fcf5f7df2f0493c437dc
シングルトンのベターな実装方法 - Qiita(ja)

のように、

コード:

class Foo
{
private:
    Foo() = default;
    ~Foo() = default;

    static Foo* instance;

public:
    Foo(const Foo&) = delete;
    Foo& operator=(const Foo&) = delete;
    Foo(Foo&&) = delete;
    Foo& operator=(Foo&&) = delete;

    static Foo& get_instance()
    {
        return *instance;
    }

    static void create()
    {
        if (!instance) {
            instance = new Foo;
        }
    }

    static void destroy()
    {
        delete instance;
        instance = nullptr;
    }
};

Foo* Foo::instance = nullptr;
のように、コンストラクタやデストラクタを pubic にしないのが、はやりのようです。
VTuber:
[香車]東上☆Aho(暎帆)☆海美
http://atassyu.php.xdomain.jp/vtuber/index.html
レスがついていないものを優先して、レスするみみ。時々、見当外れなレスしみみ。

中の人:
手提鞄あたッしュ、[MrAtassyu] 手提鞄屋魚有店
http://ameblo.jp/mratassyu/
Pixiv: 666303
Windows, Mac, Linux, Haiku, Raspbery Pi, Jetson Nano, 電子ブロック 持ち。

返信

“C言語何でも質問掲示板” へ戻る