【发布时间】:2015-05-28 00:08:20
【问题描述】:
我一直在学习 C++/SFML 教程 (http://www.gamefromscratch.com/page/Game-From-Scratch-CPP-Edition.aspx),并在完成后开始更改代码以尝试各种不同的东西,并更加熟悉 C++ 和 SFML。
对于菜单屏幕,我决定为按钮创建一个对象。为此,我创建了 Button.cpp 和 Button.h,然后在 MainMenu.h 文件中链接到 Button.h。我将 Button button_play 添加为 MainMenu 类的公共成员,但是当我调用 Button 函数(例如:button_play.ButtonInit("new-game");)时,我收到错误:error LNK2019: unresolved external symbol "public: void __thiscall Button::ButtonInit(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?ButtonInit@Button@@QAEXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function "public: enum MainMenu::MenuResult __thiscall MainMenu::Show(class sf::RenderWindow &)" (?Show@MainMenu@@QAE?AW4MenuResult@1@AAVRenderWindow@sf@@@Z)
我已经对此进行了大量搜索,我发现的大多数答案都围绕着没有正确实现类成员函数,但据我所知,我做得正确。然而,我对 C++ 很陌生,所以我可能只是遗漏了一些东西。
这是我的代码:
MainMenu.h
#pragma once
#include "SFML\Window.hpp"
#include "SFML\Graphics.hpp"
#include "GameObjectManager.h"
#include "Button.h"
#include <list>
class MainMenu
{
public:
MainMenu(){};
~MainMenu() {};
enum MenuResult { Nothing, Exit, Play };
const static GameObjectManager& GetGameObjectManager();
struct MenuItem
{
public:
sf::Rect<int> rect;
MenuResult action;
};
MenuResult Show(sf::RenderWindow& window);
static GameObjectManager _gameObjectManager;
Button button_play;
private:
MenuResult GetMenuResponse(sf::RenderWindow& window);
MenuResult HandleClick(int x, int y);
std::list<MenuItem> _menuItems;
};
MainMenu.cpp(这很长;我只包含了调用 ButtonInit() 的函数和 Show() 返回的函数 - 如果您需要查看更多信息,请告诉我,我可以包含其余的该文件的代码)
#include "stdafx.h"
#include "MainMenu.h"
#include "ServiceLocator.h"
#include "Button.h"
MainMenu::MenuResult MainMenu::Show(sf::RenderWindow& window)
{
button_play.ButtonInit("new-game");
return GetMenuResponse(window);
}
MainMenu::MenuResult MainMenu::GetMenuResponse(sf::RenderWindow& window)
{
sf::Event menuEvent;
while(42 != 43)
{
while(window.pollEvent(menuEvent))
{
if(menuEvent.type == sf::Event::MouseMoved)
{
button_play.Update(window);
}
if(menuEvent.type == sf::Event::MouseButtonPressed)
{
if(ServiceLocator::GetAudio()->IsSongPlaying())
{
ServiceLocator::GetAudio()->StopAllSounds();
}
return HandleClick(menuEvent.mouseButton.x,menuEvent.mouseButton.y);
}
if(menuEvent.type == sf::Event::Closed)
{
return Exit;
}
}
}
}
按钮.h
#pragma once
class Button
{
public:
Button() {};
~Button() {};
void ButtonInit(std::string name);
void Update(sf::RenderWindow & rw);
};
按钮.cpp
#include "StdAfx.h"
#include "Button.h"
void Button::ButtonInit(std::string name)
{
}
void Button::Update(sf::RenderWindow & rw)
{
}
stdafx.h(可能不需要看这个,但以防万一)
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
// TODO: reference additional headers your program requires here
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/Audio.hpp>
#include <map>
#include <iostream>
#include <cassert>
#include <string>
任何帮助将不胜感激。
【问题讨论】:
-
我认为这是 MSVS 中的项目的问题,我使用了“清洁解决方案”和“重建解决方案”,之后我的 Button.cpp 文件不再链接到解决方案。重新链接它消除了错误。
-
这个问题不是重复的。