【问题标题】:error LNK2019: unresolved external symbol "public: void __thiscall Button::ButtonInit" [duplicate]错误 LNK2019:未解析的外部符号“public:void __thiscall Button::ButtonInit”[重复]
【发布时间】: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 文件不再链接到解决方案。重新链接它消除了错误。
  • 这个问题不是重复的。

标签: c++ function class sfml


【解决方案1】:

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

a) 将您的方法签名更改为

void ButtonInit(const char* name);

或 b) 像这样调用你的方法:

button_play.ButtonInit(std::string("new-game"));

【讨论】:

  • 可以用字符串字面量和std::string 参数调用它。
  • 两个类都在同一个项目中(VC++ 2010),是的。我尝试了您的建议,但仍然收到原始错误
  • 好吧,错误在那个 'public: void __thiscall Button::ButtonInit(class std::basic_string,class std::allocator >)' 变成了'public: void __thiscall Button::ButtonInit(char const *)'
猜你喜欢
  • 2016-07-21
  • 2018-11-13
  • 1970-01-01
  • 2013-01-28
  • 2023-03-28
  • 2015-04-23
  • 1970-01-01
  • 2012-10-31
相关资源
最近更新 更多