错误 LNK2019:未解析的外部符号"public: void __thiscall Button::ButtonInit"
error LNK2019: unresolved external symbol "public: void __thiscall Button::ButtonInit"
我一直在学习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"));
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 在c++类上调用void函数
- 为什么野牛仍在使用"int yylex(void)",却找不到"int yylex(YYS
- 在派生函数中指定void*参数
- C++为什么尽管我调用了void函数,它却不起作用
- 如何从void函数输出字符串
- 我应该使用什么来代替void作为变体中的替代类型之一
- 奇怪的结构&GCC&clang(void*返回类型)
- Arduino:for/while/if在void setup()或void loop()之前?——错误:之前需要不合格
- 为什么这个函数将"const char*"转换为"void* const"而不是"const void*"
- 引用一个已擦除类型(void*)的指针
- 将尾部调用void(i32,..)位转换为llvm::函数以获取FnAttribute
- 错误LNK2019未解析的外部符号"public: __thiscall SLinkList<char>::SLinkList<char>(void)"
- 代数树表达式C++错误:未解析的外部符号"Public:_thiscall AlgebraicTreeExpression::~AlgebraicTreeExpression(void) ..."
- 错误 1 错误 LNK2019:未解析的外部符号"public: void __thiscall 登录::登录菜单(void)
- 错误 LNK2019:未解析的外部符号"public: void __thiscall Button::ButtonInit"
- 未解析的外部符号 "public: int myclass::get_a (void)" 如何解析此代码?菜鸟Q
- C++ 模板类错误 public: void __thiscall Number<int>::d isplayNumber(void)" (?displayNumber@?$Number@
- 错误 LNK2019:未解析的外部符号"public: __thiscall rectangle::rectangle(void)" (??0rectangle@@QAE@XZ) 在函数 _main
- 错误 LNK2019:未解析的外部符号"public: void__thiscall Start::showMenu(void)"