Thread: Unresolved External Symbol error because of a GetKeyState() call in struct method.

  1. #1
    Registered User
    Join Date
    Sep 2019
    Posts
    1

    Unresolved External Symbol error because of a GetKeyState() call in struct method.

    I'm currently in the process of learning C/C++ and I was just messing around with some projects I had in my head. This problem might be easy to fix but I have no idea about how C++ Works underneath So thats why i'm here now.

    I have a header file with a Namespace, and inside of the namespace is a Struct with a few methods:

    Code:
    #pragma once
    
    #include <iostream>
    #include <Windows.h>
    
    namespace IM {
      // Movement Controls ------------//
      const int MoveUp = VK_UP;     //-//
      const int MoveDown = VK_DOWN; //
      const int MoveLeft = VK_LEFT; //
      const int MoveRight = VK_RIGHT; //
      //-------------------------------//
    
      // Tekken Styled Action Buttons -//
      const int LeftPunch = 0x41;   //--//
      const int RightPunch = 0x53;  //--//
      const int LeftKick = 0x5A;    //--//
      const int RightKick = 0x58;   //--//
      //-------------------------------//
    
      // Start and Select. --------//
      const int Start = 0x44;       //
      const int Select = 0x43;      //
      //---------------------------//
    
      struct Instance {
        bool KeyHeld(int keyCode);
        bool KeyDown(int keyCode);
        bool KeyUp(int keyCode);
      };
    }
    Alongside this there is a .cpp file with the same name, Here's the code inside of it:

    Code:
    #include "InputManager.hpp"
    
    using namespace IM;
    
    bool Instance::KeyHeld(int KeyCode)
    {
      if (GetKeyState(KeyCode) & 0x8000) {
        return true;
      }
    }
    
    bool Instance::KeyDown(int KeyCode)
    {
      return true;
    }
    
    bool Instance::KeyUp(int KeyCode)
    {
      return true;
    }
    And My main.cpp file looks as such:

    Code:
    #include "InputManager.hpp"
    
    int main()
    {
      IM::Instance InputManager;
      while (1) {
        if (InputManager.KeyHeld(IM::Start)) {
          std::cout << "Key Down." << std::endl;
        }
      }
    }
    This is the Output produced by Microsoft Compiler:

    Unresolved External Symbol error because of a GetKeyState() call in struct method.-unknown-jpg

    I'm using Visual Studio Code with C++ Extensions. Heres how the build.bat file looks like:

    Code:
    @echo off
    if exist "C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Auxiliary\Build\vcvarsall.bat" (
        call "C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Auxiliary\Build\vcvarsall.bat" x86
    ) else (
        call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvarsall.bat" x86
    )
    set compilerflags=/Od /Zi /EHsc /std:c++latest /I include
    set linkerflags=/OUT:bin\main.exe
    cl.exe %compilerflags% src\*.cpp /link %linkerflags%
    del bin\*.ilk *.obj *.pdb
    Last edited by Salem; 09-12-2019 at 11:45 AM. Reason: Removed crayola

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    You need to link against the library that defines that windows function. According to the documentation, the library is User32.lib. I don't use windows so I'm not sure of the command line syntax, but maybe something like this:
    Code:
    set compilerflags=/Od /Zi /EHsc /std:c++latest /I include
    set linkerflags=/OUT:bin\main.exe
    set libraries=User32.lib
    cl %compilerflags% %libraries% src\*.cpp /link %linkerflags%
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error LNK2019: unresolved external symbol
    By Cannibalope in forum C++ Programming
    Replies: 3
    Last Post: 12-06-2012, 09:35 PM
  2. error LNK2019: unresolved external symbol
    By Opel_Corsa in forum C++ Programming
    Replies: 3
    Last Post: 11-16-2006, 12:12 PM
  3. error LNK2001: unresolved external symbol
    By matth in forum C++ Programming
    Replies: 1
    Last Post: 01-19-2006, 04:42 PM
  4. Error: unresolved external symbol
    By amirahasanen1 in forum C++ Programming
    Replies: 5
    Last Post: 10-07-2005, 09:21 AM

Tags for this Thread