错误 LNK2019:未解析的外部符号"public: void __thiscall Button::ButtonInit"

error LNK2019: unresolved external symbol "public: void __thiscall Button::ButtonInit"

本文关键字:void public Button ButtonInit thiscall 外部 LNK2019 错误 符号      更新时间:2023-10-16

我一直在学习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++还很陌生,所以我可能只是错过了一些东西。

这是我的代码:

主菜单.h

#pragma once
#include "SFMLWindow.hpp"
#include "SFMLGraphics.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>

如有任何帮助,我们将不胜感激。

我认为,您在同一个项目中有两个类。链接器消息告诉您,链接器找不到合适的函数定义。所以我的猜测是。。。链接器找不到函数的合适重载。"new game"是一个const字符*,它不是std::字符串。

a) 将方法签名更改为

void ButtonInit(const char* name);

或b) 调用您的方法,如:

button_play.ButtonInit(std::string("new-game"));
相关文章: