【问题标题】:Unresolved Error when calling function of Flappy Bird Clone调用 Flappy Bird Clone 的函数时出现未解决的错误
【发布时间】:2020-04-18 15:38:56
【问题描述】:

我收到错误:

错误 LNK2019 未解析的外部符号“public: struct DirectX::SimpleMath::Vector2 __thiscall Bird::getScreenPos(void)" (?getScreenPos@Bird@@QAE?AUVector2@SimpleMath@DirectX@@XZ) 引用 在函数“私有:void __thiscall Game::Render(void)”中 (?render@Game@@AAEXXZ)

当我运行我的代码时。我不知道问题是什么,我希望有人可以帮助我这里是我正在调用的方法所在的类的代码:

//
//Bird.h
//
#pragma once
class Bird
{
private:
    DirectX::SimpleMath::Vector2 screenPos;
    int yVelocity;

    const int gravity = 3;
public:
    Bird();
    ~Bird();

    void Flap();
    void Update();

    inline void Bird::setScreenPos(DirectX::SimpleMath::Vector2 newPos);
    inline DirectX::SimpleMath::Vector2 Bird::getScreenPos();
    inline void setX(float newX);
    inline void setY(float newY);
};

//
//Bird.cpp
//
#include "pch.h"
#include "Bird.h"

Bird::Bird()
{
    screenPos.y = 500;
    screenPos.x = 100;
    yVelocity = 0;
}

Bird::~Bird()
{
}

void Bird::Flap()
{
    yVelocity = 50;
}

void Bird::Update()
{
    yVelocity -= gravity;
    screenPos.y += yVelocity;
}


inline DirectX::SimpleMath::Vector2 Bird::getScreenPos()
{
    return screenPos;
}

inline void Bird::setX(float newX)
{
    screenPos.x = newX;
}

inline void Bird::setY(float newY)
{
    screenPos.y = newY;
}

inline void Bird::setScreenPos(DirectX::SimpleMath::Vector2 newPos)
{
    if (screenPos.y < 0)
        screenPos = newPos;
}

我初始化对象的代码在这个块的底部:

//
// Game.h
//

#pragma once

#include "StepTimer.h"

#include "Bird.h"


// A basic game implementation that creates a D3D11 device and
// provides a game loop.
class Game
{
public:

    Game() noexcept;
    ~Game() = default;

    Game(Game&&) = default;
    Game& operator= (Game&&) = default;

    Game(Game const&) = delete;
    Game& operator= (Game const&) = delete;

    // Initialization and management
    void Initialize(HWND window, int width, int height);

    // Basic game loop
    void Tick();

    // Messages
    void OnActivated();
    void OnDeactivated();
    void OnSuspending();
    void OnResuming();
    void OnWindowSizeChanged(int width, int height);

    // Properties
    void GetDefaultSize( int& width, int& height ) const noexcept;

private:

    void Update(DX::StepTimer const& timer);
    void Render();

    void Clear();
    void Present();

    void CreateDevice();
    void CreateResources();

    void OnDeviceLost();

    // Device resources.
    HWND                                            m_window;
    int                                             m_outputWidth;
    int                                             m_outputHeight;

    D3D_FEATURE_LEVEL                               m_featureLevel;
    Microsoft::WRL::ComPtr<ID3D11Device1>           m_d3dDevice;
    Microsoft::WRL::ComPtr<ID3D11DeviceContext1>    m_d3dContext;

    Microsoft::WRL::ComPtr<IDXGISwapChain1>         m_swapChain;
    Microsoft::WRL::ComPtr<ID3D11RenderTargetView>  m_renderTargetView;
    Microsoft::WRL::ComPtr<ID3D11DepthStencilView>  m_depthStencilView;

    // Rendering loop timer.
    DX::StepTimer                                   m_timer;

    //texture
    Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> m_texture;

    std::unique_ptr<DirectX::SpriteBatch> m_spriteBatch;
    DirectX::SimpleMath::Vector2 m_origin;

    Bird* bird;
};

真正的函数在 Game.cpp 中被调用:

bird->setX(backBufferWidth / 2.f);

谢谢!

【问题讨论】:

  • 消息以Error LNK2019 开头,这意味着编译器找到了您的库提供的Vector2 声明,但链接器找不到它的定义。检查您如何在 IDE 中添加库。一般来说,库有一个用于编译器的include 文件夹和一个用于链接器的lib 文件夹。两者都应该在项目设置中的某个地方指定

标签: c++ directx flappy-bird-clone


【解决方案1】:
inline DirectX::SimpleMath::Vector2 Bird::getScreenPos()
{
    return screenPos;
}

删除inline 或将其移至头文件。

如果函数定义在头文件中,那么它应该是内联的,如果不是,那么它不应该。

【讨论】: