LNK2005エラーが出た。

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
アバター
keito94
記事: 264
登録日時: 8年前
連絡を取る:

LNK2005エラーが出た。

#1

投稿記事 by keito94 » 8年前

連投すいません。「LNK2005 "class CFmfMap MapObj" (?MapObj@@3VCFmfMap@@A) は既に CharMove.obj で定義されています。」というエラーが出て困ってます。ソースコードは添付しておいたので、そのコードを見てどこに原因があるかを教えて下さい。
ちなみにエラーログは以下のとおりです

コード:

1>CharMove.cpp
1>コードを生成中...
1>コンパイル中...
1>Main.cpp
1>コードを生成中...
1>Main.obj : error LNK2005: "class CFmfMap MapObj" (?MapObj@@3VCFmfMap@@A) は既に CharMove.obj で定義されています。
1>CharMove.obj : error LNK2019: 未解決の外部シンボル "public: __thiscall CFmfMap::CFmfMap(void)" (??0CFmfMap@@QAE@XZ) が関数 "void __cdecl `dynamic initializer for 'MapObj''(void)" (??__EMapObj@@YAXXZ) で参照されました。
1>Main.obj : error LNK2001: 外部シンボル ""public: __thiscall CFmfMap::CFmfMap(void)" (??0CFmfMap@@QAE@XZ)" は未解決です。
1>CharMove.obj : error LNK2019: 未解決の外部シンボル "public: __thiscall CFmfMap::~CFmfMap(void)" (??1CFmfMap@@QAE@XZ) が関数 "void __cdecl `dynamic atexit destructor for 'MapObj''(void)" (??__FMapObj@@YAXXZ) で参照されました。
1>Main.obj : error LNK2001: 外部シンボル ""public: __thiscall CFmfMap::~CFmfMap(void)" (??1CFmfMap@@QAE@XZ)" は未解決です。
1>CharMove.obj : error LNK2019: 未解決の外部シンボル "public: int __thiscall CFmfMap::GetValue(unsigned char,unsigned long,unsigned long)const " (?GetValue@CFmfMap@@QBEHEKK@Z) が関数 "public: struct tagPOINT __thiscall CCharMove::CheckMap(int,int)" (?CheckMap@CCharMove@@QAE?AUtagPOINT@@HH@Z) で参照されました。
1>CharMove.obj : error LNK2019: 未解決の外部シンボル "public: unsigned long __thiscall CFmfMap::GetMapWidth(void)const " (?GetMapWidth@CFmfMap@@QBEKXZ) が関数 "public: void __thiscall CCharMove::MapDraw(void)" (?MapDraw@CCharMove@@QAEXXZ) で参照されました。
1>CharMove.obj : error LNK2019: 未解決の外部シンボル "public: unsigned long __thiscall CFmfMap::GetMapHeight(void)const " (?GetMapHeight@CFmfMap@@QBEKXZ) が関数 "public: void __thiscall CCharMove::MapDraw(void)" (?MapDraw@CCharMove@@QAEXXZ) で参照されました。
1>CharMove.obj : error LNK2019: 未解決の外部シンボル "public: unsigned char __thiscall CFmfMap::GetChipWidth(void)const " (?GetChipWidth@CFmfMap@@QBEEXZ) が関数 "public: struct tagPOINT __thiscall CCharMove::CheckMap(int,int)" (?CheckMap@CCharMove@@QAE?AUtagPOINT@@HH@Z) で参照されました。
1>CharMove.obj : error LNK2019: 未解決の外部シンボル "public: unsigned char __thiscall CFmfMap::GetChipHeight(void)const " (?GetChipHeight@CFmfMap@@QBEEXZ) が関数 "public: struct tagPOINT __thiscall CCharMove::CheckMap(int,int)" (?CheckMap@CCharMove@@QAE?AUtagPOINT@@HH@Z) で参照されました。
1>CharMove.obj : error LNK2001: 外部シンボル ""int MapBitCount" (?MapBitCount@@3HA)" は未解決です。
1>Main.obj : error LNK2001: 外部シンボル ""int MapBitCount" (?MapBitCount@@3HA)" は未解決です。
1>Main.obj : error LNK2019: 未解決の外部シンボル "public: int __thiscall CFmfMap::Open(char const *)" (?Open@CFmfMap@@QAEHPBD@Z) が関数 "public: int __thiscall CGameMain::MapLoad(char const *,int)" (?MapLoad@CGameMain@@QAEHPBDH@Z) で参照されました。
1>Main.obj : error LNK2019: 未解決の外部シンボル "public: unsigned char __thiscall CFmfMap::GetLayerCount(void)const " (?GetLayerCount@CFmfMap@@QBEEXZ) が関数 "public: int __thiscall CGameMain::MapLoad(char const *,int)" (?MapLoad@CGameMain@@QAEHPBDH@Z) で参照されました。
1>Main.obj : error LNK2019: 未解決の外部シンボル "public: unsigned char __thiscall CFmfMap::GetLayerBitCount(void)const " (?GetLayerBitCount@CFmfMap@@QBEEXZ) が関数 "public: int __thiscall CGameMain::MapLoad(char const *,int)" (?MapLoad@CGameMain@@QAEHPBDH@Z) で参照されました。
添付ファイル
ソースコード一式.zip
(12.88 KiB) ダウンロード数: 145 回
デバッグは投げ捨てるものではない。
今までの質問でこれは学んだこと。
質問する時は、必ずちゃんと調べた上に問題をもとにした仕様書を作ってから質問すること。
仕様書の大切さを改めて思い知った…。

アバター
keito94
記事: 264
登録日時: 8年前
連絡を取る:

Re: LNK2005エラーが出た。

#2

投稿記事 by keito94 » 8年前

ダウンロードしたくない方のためにソースコード全体を上げてみたいと思います。
Task.h

コード:

#pragma once

class Task {
public:
	virtual ~Task() {}
	virtual void Initialize() {}     //初期化処理は実装してもしなくてもいい
	virtual void Finalize() {}     //終了処理は実装してもしなくてもいい
	virtual void Update() = 0;   //更新処理は必ず継承先で実装する
	virtual void Draw() = 0;   //描画処理は必ず継承先で実装する
};
Mapping.h

コード:

#pragma once
#include "fmfmap.h"
CFmfMap MapObj;
extern int MapBitCount;
Main.h

コード:

#pragma once
#include "DxLib.h"
class CTaskList;
class CPlayer;
class CGameMain {
	int Time;
	CPlayer* Player;
public:
	int MapLoad(const char* file_name, int stage_num);
	int Init();
	void Main();
	void Fin();
};
Main.cpp

コード:

#include "Main.h"
#include "Player.h"
#include "Mapping.h"

CGameMain Main;

int CGameMain::MapLoad(const char* file_name, int stage_num) {
	MapBitCount = MapObj.GetLayerBitCount() == 8 ? 16 : 256;
	if (!MapObj.Open(file_name)) {
		return -1;
	}
	BYTE* layer = (BYTE*)MapObj.GetLayerCount();
	if (layer == NULL) {
		return -1;
	}
	return 0;
}

int CGameMain::Init() {
	ChangeWindowMode(TRUE), SetGraphMode(320, 240, 32);
	//DXライブラリの初期化エラーが発生したときに終了。
	if (DxLib_Init() == -1) { return -1; }
	Time = GetNowCount();
	SetDrawScreen(DX_SCREEN_BACK);
	SetWaitVSyncFlag(FALSE);
	Player->Init();
	return 0;
}

void CGameMain::Main() {
	Time = GetNowCount();
	//1/60秒立つまで待つ
	while(GetNowCount()-Time<1000/60){}
	Player->Update();
	Player->Draw();
}

void CGameMain::Fin() {
	Player->Fin();
	DxLib_End();
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
	Main.Init();
	while (ScreenFlip() == 0 && ProcessMessage() == 0 && ClearDrawScreen() == 0) {
		Main.Main();
	}
	Main.Fin();
	return 0;
}

CharMove.cpp

コード:

#include "DxLib.h"
#include "Main.h"
#include "CharMove.h"
#include "Mapping.h"

CCharMove::CCharMove(POINT pt[], float clx, float crx, float cty, float cby, int coltype)
	: Clx(clx), Crx(crx), Cty(cty), Cby(cby),ColType(coltype)
{
	Init();
	for (int i = 0; i < 8; i++) {
		mPt[i].x = pt[i].x;
		mPt[i].y = pt[i].y;
	}
}

void CCharMove::Init() {
	ZeroMemory(mPt, sizeof(mPt));
}

POINT CCharMove::CheckMap(int x, int y) {
	POINT pt[] = {
		{ x + mPt[0].x,	y + mPt[0].y },//左上
		{ x + mPt[1].x,	y + mPt[1].y },//上真ん中
		{ x + mPt[2].x,	y + mPt[2].y },//右上

		{ x + mPt[3].x,	y + mPt[3].y },//左真ん中
		{ x + mPt[4].x,	y + mPt[4].y },//右真ん中

		{ x + mPt[5].x,	y + mPt[5].y },//左下
		{ x + mPt[6].x,	y + mPt[6].y },//下真ん中
		{ x + mPt[7].x,	y + mPt[7].y },//右下
	};
	POINT res = { x, y };
	DWORD cwidth = MapObj.GetChipWidth();
	DWORD cheight = MapObj.GetChipHeight();

	for (int i = 0; i < 8; i++) {
		int index = MapObj.GetValue(1, pt[i].x / MapCell, pt[i].y / MapCell);
		int src_x = (index % MapBitCount) * cwidth;
		int src_y = (index / MapBitCount) * cheight;
		if (src_x == 0 && src_y == 0) // 取得したindexが壁だったら
		{
			res.x = pt[i].x / MapCell; // 壁の座標を代入
			res.y = pt[i].y / MapCell;
			return res; // 壁の座標を返す
		}
	}
	res.x = -1;
	res.y = -1;
	return res;
}

void CCharMove::MapDraw() {
	DWORD swidth = MapObj.GetMapWidth();
	DWORD sheight = MapObj.GetMapHeight();
	DWORD cwidth = MapObj.GetChipWidth();
	DWORD cheight = MapObj.GetChipHeight();
	DWORD i, j;
	int index, src_x, src_y;
	for (i = 0; i < sheight / MapCell; i++) {
		for (j = 0; j = swidth / MapCell; i++) {
			index = MapObj.GetValue(0, i, j);
			src_x = (index % MapBitCount) * cwidth;
			src_y = (index / MapBitCount) * cheight;
		}
	}
}
CharMove.h

コード:

#pragma once
//マップチップ
const int MapCell = 16;
class CCharMove {
	float Clx, Crx, Cty, Cby;
	POINT mPt[8];
	int ColType;
public:
	CCharMove(POINT pt[], float clx, float crx, float cty, float cby,int coltype);
	void Init();
	POINT CheckMap(int x, int y);
	void MapDraw();
};
Player.h

コード:

#pragma once
#include "Mover.h"

class CCharMove;
//プレイヤーのクラスを宣言する。
class CPlayer : CMover{
protected:
	//変数を宣言する。
	float X, Y;
	float VX, VY;
	int CX, CY;
	char DashB, DashR, Dash;
	int Dir, DashCnt,dir;
	int Input, EdgeInput;
	int Graph;
	CCharMove* ColObj;
//関数を宣言する。
public:
	CPlayer(float x,float y);
	//プレイヤーの初期化。
	void Init(void);
	//プレイヤーの動きを計算。
	void Update(void);
	//プレイヤーの描画。
	void Draw(void);
	//プレイヤーの終了処理。
	void Fin(void);
};
Player.cpp

コード:

#include "DxLib.h"
#include "Player.h"
#include "CharMove.h"

CPlayer::CPlayer(float x, float y)
	:CMover(x, y, -16, -16, 16, 16) {
	POINT pt[] =
	{
		{ (LONG)L,	(LONG)T },//左上
		{ 0,			(LONG)T },//上真ん中
		{ (LONG)R,	(LONG)T },//右上

		{ (LONG)L,	0 },//左真ん中
		{ (LONG)R,	0 },//右真ん中

		{ (LONG)L,	(LONG)B },//左下
		{ 0,			(LONG)B },//下真ん中
		{ (LONG)R,	(LONG)B },//右下
	};
	ColObj = new CCharMove(pt, 0, 0, 0, 0, 0);
}

void CPlayer::Init(void) {
	//画像を読み込む。
	X = 160.0F, Y = 240.0F;
	Graph = LoadGraph("テスト用.png");
	Dir = FALSE;
	Dash = FALSE;
}

void CPlayer::Update(void) {
	{
		int i;
		i = GetJoypadInputState(DX_INPUT_KEY_PAD1);
		EdgeInput = i & ~Input;
		Input = i;
	}
	VX = 0.0F;
	//キャラの動きを入れる。
	if ((Input&PAD_INPUT_RIGHT)) {
		Dir = FALSE;
		VX = 2.5F;
	}
	if ((Input&PAD_INPUT_LEFT)) {
		Dir = TRUE;
		VX = -2.5F;
	}
	X += VX;
}

void CPlayer::Draw(void) {
	//描画をする。
	DrawRotaGraph((int)X + 8, (int)Y - 8, 1.0, 0.0, Graph, TRUE, Dir);
}

void CPlayer::Fin(void) {
	//画像を削除する。
	DeleteGraph(Graph);
}
デバッグは投げ捨てるものではない。
今までの質問でこれは学んだこと。
質問する時は、必ずちゃんと調べた上に問題をもとにした仕様書を作ってから質問すること。
仕様書の大切さを改めて思い知った…。

アバター
みけCAT
記事: 6734
登録日時: 14年前
住所: 千葉県
連絡を取る:

Re: LNK2005エラーが出た。

#3

投稿記事 by みけCAT » 8年前

変数MapObjが(複数のソースコードからincludeされる)ヘッダファイルで定義されてしまっているのがダメですね。
その下の行にあるMapBitCountと同様に、ヘッダではexternを付けた宣言だけをするのがいいでしょう。
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

アバター
keito94
記事: 264
登録日時: 8年前
連絡を取る:

Re: LNK2005エラーが出た。

#4

投稿記事 by keito94 » 8年前

みけCATさんに言われてexternをつけた宣言を試してみたところ、こんなエラーコードが出ました。

コード:

1>Main.cpp
1>CharMove.obj : error LNK2019: 未解決の外部シンボル "public: int __thiscall CFmfMap::GetValue(unsigned char,unsigned long,unsigned long)const " (?GetValue@CFmfMap@@QBEHEKK@Z) が関数 "public: struct tagPOINT __thiscall CCharMove::CheckMap(int,int)" (?CheckMap@CCharMove@@QAE?AUtagPOINT@@HH@Z) で参照されました。
1>CharMove.obj : error LNK2019: 未解決の外部シンボル "public: unsigned long __thiscall CFmfMap::GetMapWidth(void)const " (?GetMapWidth@CFmfMap@@QBEKXZ) が関数 "public: void __thiscall CCharMove::MapDraw(void)" (?MapDraw@CCharMove@@QAEXXZ) で参照されました。
1>CharMove.obj : error LNK2019: 未解決の外部シンボル "public: unsigned long __thiscall CFmfMap::GetMapHeight(void)const " (?GetMapHeight@CFmfMap@@QBEKXZ) が関数 "public: void __thiscall CCharMove::MapDraw(void)" (?MapDraw@CCharMove@@QAEXXZ) で参照されました。
1>CharMove.obj : error LNK2019: 未解決の外部シンボル "public: unsigned char __thiscall CFmfMap::GetChipWidth(void)const " (?GetChipWidth@CFmfMap@@QBEEXZ) が関数 "public: struct tagPOINT __thiscall CCharMove::CheckMap(int,int)" (?CheckMap@CCharMove@@QAE?AUtagPOINT@@HH@Z) で参照されました。
1>CharMove.obj : error LNK2019: 未解決の外部シンボル "public: unsigned char __thiscall CFmfMap::GetChipHeight(void)const " (?GetChipHeight@CFmfMap@@QBEEXZ) が関数 "public: struct tagPOINT __thiscall CCharMove::CheckMap(int,int)" (?CheckMap@CCharMove@@QAE?AUtagPOINT@@HH@Z) で参照されました。
1>CharMove.obj : error LNK2001: 外部シンボル ""int MapBitCount" (?MapBitCount@@3HA)" は未解決です。
1>Main.obj : error LNK2001: 外部シンボル ""int MapBitCount" (?MapBitCount@@3HA)" は未解決です。
1>Main.obj : error LNK2019: 未解決の外部シンボル "public: __thiscall CFmfMap::CFmfMap(void)" (??0CFmfMap@@QAE@XZ) が関数 "void __cdecl `dynamic initializer for 'MapObj''(void)" (??__EMapObj@@YAXXZ) で参照されました。
1>Main.obj : error LNK2019: 未解決の外部シンボル "public: __thiscall CFmfMap::~CFmfMap(void)" (??1CFmfMap@@QAE@XZ) が関数 "void __cdecl `dynamic atexit destructor for 'MapObj''(void)" (??__FMapObj@@YAXXZ) で参照されました。
1>Main.obj : error LNK2019: 未解決の外部シンボル "public: int __thiscall CFmfMap::Open(char const *)" (?Open@CFmfMap@@QAEHPBD@Z) が関数 "public: int __thiscall CGameMain::MapLoad(char const *,int)" (?MapLoad@CGameMain@@QAEHPBDH@Z) で参照されました。
1>Main.obj : error LNK2019: 未解決の外部シンボル "public: unsigned char __thiscall CFmfMap::GetLayerCount(void)const " (?GetLayerCount@CFmfMap@@QBEEXZ) が関数 "public: int __thiscall CGameMain::MapLoad(char const *,int)" (?MapLoad@CGameMain@@QAEHPBDH@Z) で参照されました。
1>Main.obj : error LNK2019: 未解決の外部シンボル "public: unsigned char __thiscall CFmfMap::GetLayerBitCount(void)const " (?GetLayerBitCount@CFmfMap@@QBEEXZ) が関数 "public: int __thiscall CGameMain::MapLoad(char const *,int)" (?MapLoad@CGameMain@@QAEHPBDH@Z) で参照されました。
1>C:\Users\keito940\documents\visual studio 2017\Projects\スタアド\Debug\スタアド.exe : fatal error LNK1120: 11 件の未解決の外部参照
デバッグは投げ捨てるものではない。
今までの質問でこれは学んだこと。
質問する時は、必ずちゃんと調べた上に問題をもとにした仕様書を作ってから質問すること。
仕様書の大切さを改めて思い知った…。

YuO
記事: 947
登録日時: 14年前
住所: 東京都世田谷区

Re: LNK2005エラーが出た。

#5

投稿記事 by YuO » 8年前

エラーになった関数を定義していないか,定義されているファイルをリンクしていないのでしょう。
CFmfMapクラスの実装のある.cppファイルはプロジェクトに追加されていますか。

アバター
keito94
記事: 264
登録日時: 8年前
連絡を取る:

Re: LNK2005エラーが出た。

#6

投稿記事 by keito94 » 8年前

あっそうか、と思い、fmfmap.cppをプロジェクトに追加して、MapBitCountを宣言してみたところ解決しました。
ありがとうございました。
デバッグは投げ捨てるものではない。
今までの質問でこれは学んだこと。
質問する時は、必ずちゃんと調べた上に問題をもとにした仕様書を作ってから質問すること。
仕様書の大切さを改めて思い知った…。

返信

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