VS2019编译错误,无法解析的外部符号

0

系统:windows10 专业工作站版

IDE:VS2019

Easyx版本:20210224

尝试过修改字符集,重新安装easyx

#include <graphics.h>		// 引用图形库头文件
#include <conio.h>
#include<map>
#include <string>
using namespace std;

class Graphicstool {
public:
	static map<string, IMAGE> ImagePool;
	Graphicstool() {}

	static void createWindow(int px, int py) {
		initgraph(px, py);
	}

	static bool addImage(string name, string imgPath) {
		IMAGE img;
		loadimage(&img, imgPath.c_str());
		std::pair<std::map<string, IMAGE>::iterator, bool> result;
		result = Graphicstool::ImagePool.insert(std::pair<string, IMAGE>(name, img));
		if (result.second == false) {
			return false;
		}
		return true;
	}

	static bool showImage(int px, int py, string name) {
		map<string, IMAGE>::iterator iter;
		iter = Graphicstool::ImagePool.find(name);
		if (iter != Graphicstool::ImagePool.end()) {
			putimage(px, py, &(iter->second));
			return true;
		}
		else {
			return false;
		}
	}

	static void clearScreen() {
		cleardevice();
	}
};

class Player {
public:
	string imgPath;

	Player(string imgPath) {
		this->imgPath = imgPath;
		this->px = 0;
		this->py = 0;
		Graphicstool::addImage("Player", this->imgPath);
	}

	~Player() {
		Graphicstool::ImagePool.erase("Player");
	}

	string MoveBuffer;
	int px;
	int py;

	void placePlayer(int px, int py) {
		this->px = px;
		this->py = py;
	}

	void showPlayer() {
		Graphicstool::showImage(px, py, "Player");
	}

	void movePlayer(string forward) {
		MoveBuffer = forward;
		if (MoveBuffer == "right") {
			this->px += 10;
			this->py += 10;
			showPlayer();
		}
	}
};

class Controller {
public:
	double FrameSpeed;
	Player* PlayerCTL;

	Controller() {
		FrameSpeed = 1000 / 60;
	}

	void createPlayer(string imgPath) {
		Player* p = new Player(imgPath);
		PlayerCTL = &*p;
	}

	void deletePlayer() {
		PlayerCTL->~Player();
		delete PlayerCTL;
	}

	void run() {
		Start();
		while (true) {
			Update();
			Sleep(FrameSpeed);
		}
		closegraph();
	}

	void Start() {
		initgraph(640, 480);	// 创建绘图窗口,大小为 640x480 像素
		keyboardListener();
	}

	void Update() {
		PlayerCTL->movePlayer("BUFFER");
	}

	void keyboardListener() {
		char input;
		while (true) {
			if (_kbhit()) {
				input = _kbhit();
				switch (input) {
				default:
					break;
				case 119:// 119 = w
					PlayerCTL->movePlayer("up");
					break;
				case 97:// 97 = a
					PlayerCTL->movePlayer("left");
					break;
				case 115:// 115 = s
					PlayerCTL->movePlayer("down");
					break;
				case 100:// 100 = d
					PlayerCTL->movePlayer("right");
					break;
				}
			}
		}
	}

};

int main()
{
	Controller ctl;
	ctl.run();
	return 0;
}

错误 LNK2001 无法解析的外部符号 "public: static class std::map<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class IMAGE,struct std::less<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >,class std::allocator<struct std::pair<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const ,class IMAGE> > > Graphicstool::ImagePool" (?ImagePool@Graphicstool@@2V?$map@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@VIMAGE@@U?$less@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@V?$allocator@U?$pair@$$CBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@VIMAGE@@@std@@@2@@std@@A) rlgame C:\Users\zhouxiang289\source\repos\rlgame\rlgame.obj 1

0

根据你的错误提示,这个错误与字符集和 EasyX 都没有关系,这个错误出现在一个 map 成员上。

再根据你的代码,可以定位到错误应该是关于 class Graphicstool 的成员 static map<string, IMAGE> ImagePool,错误原因为“无法解析的外部符号”。

所以很简单,你声明了成员 ImagePool,却没有定义,所以才会找不到这个外部符号。

解决方法:增加静态成员的定义:

class Graphicstool
{
public:
	static map<string, IMAGE> ImagePool;

	……
};

map<string, IMAGE> Graphicstool::ImagePool = {};
ava
慢羊羊

2021-6-24

技术讨论社区