By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,913 Members | 1,379 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,913 IT Pros & Developers. It's quick & easy.

Need help with a Lnk2019 error in an SFML/C++ project

P: 3
I'm currently making a 2D video game using the SFML library on Visual Studio 2019 community with the help of a Youtube tutorial series by Suraj Sharma:

https://www.youtube.com/watch?v=IdKZpv6xqdw&list=PL6xSOsbVA1ebkU66okpi-KViAO8_9DJKg&index=1

Recently i'm making a drop list of buttons to be implemented in the game menu's settings(Video 44 to 48)

For example,when i click on the menu's 'Settings' button the menu will switch into a 'Settings' state that comprises a drop list of resolution options for me to choose from.

Here's the code:

Gui.h
Expand|Select|Wrap|Line Numbers
  1. #pragma once
  2. #ifndef GUI_H
  3. #define GUI_H
  4.  
  5. #include "pch.h"
  6.  
  7. enum BStates { IDLE = 0,HOVER,ACTIVE };
  8.  
  9. namespace gui {
  10.     class Button
  11.     {
  12.     private:
  13.         short unsigned bStates;
  14.         short unsigned Id;
  15.  
  16.         sf::RectangleShape Shape;
  17.         sf::Font* Font;
  18.         sf::Text Text;
  19.  
  20.         sf::Color TextIdl;
  21.         sf::Color TextHover;
  22.         sf::Color TextActive;
  23.  
  24.         sf::Color Idle;
  25.         sf::Color Hover;
  26.         sf::Color Active;
  27.  
  28.         sf::Color OutlIdle;
  29.         sf::Color OutlHover;
  30.         sf::Color OutlActive;
  31.     public:
  32.         Button(float x, float y, float w, float h,
  33.             sf::Font* Font, std::string Text,
  34.             unsigned CharSize, sf::Color TextIdl, sf::Color TextHover, sf::Color TextActive,
  35.             sf::Color Idle, sf::Color Hover, sf::Color Active
  36.             ,sf::Color OutlIdle, //= sf::Color::Transparent
  37.                 ,sf::Color OutlHover = sf::Color::Transparent, 
  38.             sf::Color OutlActive = sf::Color::Transparent,
  39.             short unsigned Id = 0);
  40.         ~Button();
  41.  
  42.         //Access
  43.         const bool Pressed()const;
  44.         const std::string GetText()const;
  45.         const short unsigned& GetId()const;
  46.         //Modifier
  47.         void SetText(const std::string Text);
  48.         void SetId(const short unsigned Id);
  49.         //Function
  50.         void Update(const sf::Vector2f& MousePos);
  51.         void Render(sf::RenderTarget& target);
  52.     };
  53.     class DrpList {
  54.     private:
  55.         float Keytime;
  56.         float KeytimeMax;
  57.         sf::Font& Fnt;
  58.         gui::Button* ActivElmnt;
  59.         std::vector<gui::Button*> List;
  60.         bool ShowList;
  61.     public:
  62.         DrpList(float x, float y, float w, float h, sf::Font& Fnt, std::string List[], unsigned NumElemnt, unsigned DefIndex = 0);
  63.         ~DrpList();
  64.  
  65.         //Funcs
  66.         const bool GetKeytime();
  67.         void UpdateKeytime(const float& dt);
  68.         void Update(const sf::Vector2f& MousePos, const float& dt);
  69.         void Render(sf::RenderTarget& target);
  70.     };
  71. }
  72. #endif // !BUTTON_H
Gui.cpp
Expand|Select|Wrap|Line Numbers
  1. #include "Gui.h"
  2.  
  3. using namespace gui;
  4.  
  5. Button::Button(float x, float y, float w, float h,
  6.     sf::Font* Font, std::string Text, unsigned CharSize,
  7.     sf::Color TextIdl, sf::Color TextHover, sf::Color TextActive,
  8.     sf::Color Idle, sf::Color Hover, sf::Color Active
  9.    ,sf::Color OutlIdle, sf::Color OutlHover, sf::Color OutlActive, short unsigned Id)
  10. {
  11.     this->bStates = IDLE;
  12.     this->Id = Id;
  13.  
  14.     this->Shape.setPosition(sf::Vector2f(x, y));
  15.     this->Shape.setSize(sf::Vector2f(w, h));
  16.     this->Shape.setFillColor(Idle);
  17.     this->Shape.setOutlineThickness(1.f);
  18.     this->Shape.setOutlineColor(OutlIdle);
  19.  
  20.     this->Font = Font;
  21.  
  22.     this->Text.setFont(*this->Font);
  23.     this->Text.setString(Text);
  24.     this->Text.setFillColor(TextIdl);
  25.     this->Text.setCharacterSize(CharSize);
  26.     this->Text.setPosition(this->Shape.getPosition().x + (this->Shape.getGlobalBounds().width / 2.f) - this->Text.getGlobalBounds().width / 2.f,
  27.                            this->Shape.getPosition().y + (this->Shape.getGlobalBounds().height / 2.f) - this->Text.getGlobalBounds().height / 2.f);
  28.     this->TextIdl = TextIdl;
  29.     this->TextHover = TextHover;
  30.     this->TextActive = TextActive;
  31.  
  32.     this->Idle = Idle;
  33.     this->Hover = Hover;
  34.     this->Active = Active;
  35.  
  36.     this->OutlIdle = OutlIdle;
  37.     /*this->OutlHover = OutlHover;
  38.     this->OutlActive = OutlActive;*/
  39. }
  40.  
  41. Button::~Button()
  42. {
  43. }
  44. //Access
  45. const bool Button::Pressed() const
  46. {
  47.     if (this->bStates == ACTIVE)
  48.         return true;
  49.     return false;
  50. }
  51.  
  52. const std::string gui::Button::GetText() const
  53. {
  54.     return this->Text.getString();
  55. }
  56. const short unsigned& gui::Button::GetId() const
  57. {
  58.     return this->Id;
  59. }
  60. //Modifier
  61. void gui::Button::SetText(const std::string Text)
  62. {
  63.     this->Text.setString(Text);
  64. }
  65.  
  66. void gui::Button::SetId(const short unsigned Id)
  67. {
  68.     this->Id = Id;
  69. }
  70.  
  71. //Functions
  72.  
  73. void Button::Update(const sf::Vector2f& MousePos)
  74. {
  75.     this->bStates = IDLE;
  76.  
  77.     if (this->Shape.getGlobalBounds().contains(MousePos)) {
  78.         this->bStates = HOVER;
  79.         if (sf::Mouse::isButtonPressed(sf::Mouse::Left)){
  80.             this->bStates = ACTIVE;
  81.         }
  82.     }
  83.     switch (this->bStates)
  84.     {
  85.     case IDLE:
  86.         this->Shape.setFillColor(this->Idle);
  87.         this->Text.setFillColor(this->TextIdl);
  88.         this->Shape.setOutlineColor(this->OutlIdle);
  89.         break;
  90.     case HOVER:
  91.         this->Shape.setFillColor(this->Hover);
  92.         this->Text.setFillColor(this->TextHover);
  93.         this->Shape.setOutlineColor(this->OutlHover);
  94.         break;
  95.     case ACTIVE:
  96.         this->Shape.setFillColor(this->Active);
  97.         this->Text.setFillColor(this->TextActive);
  98.         this->Shape.setOutlineColor(this->OutlActive);
  99.         break;
  100.     default:
  101.         this->Shape.setFillColor(sf::Color::White);
  102.         this->Text.setFillColor(sf::Color::Blue);
  103.         this->Shape.setOutlineColor(sf::Color::Green);
  104.         break;
  105.     }
  106. }
  107.  
  108. void Button::Render(sf::RenderTarget& target)
  109. {
  110.     target.draw(this->Shape);
  111.     target.draw(this->Text);
  112. }
  113.  
  114. //-------------DrpList---------------
  115. gui::DrpList::DrpList(float x, float y, float w, float h, sf::Font& Fnt, std::string List[], unsigned NumElemnt, unsigned DefIndex)
  116.     :Fnt(Fnt), ShowList(false), KeytimeMax(10.f), Keytime(0.f)
  117. {
  118.     //unsigned NumElemnt = sizeof(List) / sizeof(std::string);
  119.     this->ActivElmnt = new gui::Button(
  120.         x, y, w, h,
  121.         &this->Fnt, List[DefIndex], 12,
  122.         sf::Color(255, 255, 255, 150),
  123.         sf::Color(255, 255, 255, 200),
  124.         sf::Color(20, 20, 20, 50),
  125.         sf::Color(70, 70, 70, 200),
  126.         sf::Color(150, 150, 150, 200),
  127.         sf::Color(20, 20, 20, 200),
  128.         sf::Color(255, 255, 255, 200)
  129.         sf::Color(255, 255, 255, 255),
  130.         sf::Color(20, 20, 20, 200));
  131.  
  132.     for (size_t i = 0; i < NumElemnt; i++) {
  133.         this->List.push_back(new gui::Button(
  134.             x, y + ((i + 1) * h), w, h,
  135.             &this->Fnt, List[i], 12,
  136.             sf::Color(255, 255, 255, 150),
  137.             sf::Color(255, 255, 255, 255),
  138.             sf::Color(20, 20, 20, 50),
  139.             sf::Color(70, 70, 70, 200),
  140.             sf::Color(150, 150, 150, 200),
  141.             sf::Color(20, 20, 20, 200),
  142.             sf::Color(255, 255, 255, 0)
  143.             sf::Color(255, 255, 255, 0),
  144.             sf::Color(20, 20, 20, 0),i));
  145.     }
  146. }
  147.  
  148.  
  149. gui::DrpList::~DrpList()
  150. {
  151.     delete this->ActivElmnt;
  152.     for (size_t i = 0; i < this->List.size(); i++) {
  153.         delete this->List[i];
  154.     }
  155. }
  156.  
  157. const bool gui::DrpList::GetKeytime()
  158. {
  159.     if (this->Keytime >= this->KeytimeMax) {
  160.         this->Keytime = 0.f;
  161.         return true;
  162.     }
  163.     return false;
  164. }
  165.  
  166. void gui::DrpList::UpdateKeytime(const float& dt)
  167. {
  168.     if (this->Keytime < this->KeytimeMax)
  169.         this->Keytime += 10.f * dt;
  170. }
  171.  
  172. //DrpList
  173. void gui::DrpList::Update(const sf::Vector2f& MousePos , const float& dt)
  174. {
  175.     this->UpdateKeytime(dt);
  176.     this->ActivElmnt->Update(MousePos);
  177.     //Show - hide list
  178.     if (this->ActivElmnt->Pressed() && this->GetKeytime()) {
  179.         if (this->ShowList)
  180.             this->ShowList = false;
  181.         else this->ShowList = true;
  182.     }
  183.  
  184.     if (this->ShowList) {
  185.         for (auto& i : this->List) {
  186.             i->Update(MousePos);
  187.  
  188.             if (i->Pressed() && this->GetKeytime()) {
  189.                 this->ShowList = false;
  190.                 this->ActivElmnt->SetText(i->GetText());
  191.                 this->ActivElmnt->SetId(i->GetId());
  192.             }
  193.         }
  194.     }
  195. }
  196.  
  197. void gui::DrpList::Render(sf::RenderTarget& target)
  198. {
  199.     this->ActivElmnt->Render(target);
  200.     if (this->ShowList) {
  201.         for (auto& i : this->List) {
  202.             i->Render(target);
  203.         }
  204.     }
  205. }
Settings.h

Expand|Select|Wrap|Line Numbers
  1. #pragma once
  2. #ifndef SETTINGS_H
  3. #define SETTINGS_H
  4.  
  5. #include "State.h"
  6. #include "Gui.h"
  7.  
  8. class Settings :
  9.     public State
  10. {
  11. private:
  12.     //Vars
  13.     sf::Font Fnt;
  14.     sf::Texture BgTex;
  15.     sf::RectangleShape BackGrnd;
  16.  
  17.     std::map<std::string, gui::Button*> buttons;
  18.     std::map<std::string, gui::DrpList*> drplist;
  19.     gui::DrpList* Dl;
  20.  
  21.     //Functions
  22.     void InitVars();
  23.     void InitBackGrnd();
  24.     void InitFonts();
  25.     void InitKeybinds();
  26.     void InitGui();
  27.  
  28. public:
  29.     Settings(sf::RenderWindow* window, std::map<std::string, int>* SupportedKeys, std::stack<State*>* states);
  30.     virtual~Settings();
  31.     //Access
  32.     //Funcs
  33.     void UpdateInput(const float& dt);
  34.     void Update(const float& dt);
  35.     void UpdateGui(const float& dt);
  36.  
  37.     void RenderGui(sf::RenderTarget& target);
  38.     void Render(sf::RenderTarget* target = nullptr);
  39. };
  40. #endif // !SETTINGS_H
Settings.cpp

Expand|Select|Wrap|Line Numbers
  1. #include "Settings.h"
  2.  
  3. //Init Funcs
  4. void Settings::InitVars()
  5. {
  6.  
  7. }
  8.  
  9. void Settings::InitBackGrnd()
  10. {
  11.     this->BackGrnd.setSize(
  12.         sf::Vector2f(
  13.             static_cast<float>(this->window->getSize().x),
  14.             static_cast<float>(this->window->getSize().y)));
  15.  
  16.     if (!this->BgTex.loadFromFile("Resources/Images/BackGrounds/Menu/M2.png")) {
  17.         throw "Error:Main_Menu:Failed to load background texture";
  18.     }
  19.     this->BackGrnd.setTexture(&this->BgTex);
  20. }
  21.  
  22. void Settings::InitFonts()
  23. {
  24.     if (!this->Fnt.loadFromFile("Fonts/SPACEMAN.ttf")) {
  25.         throw("Error:Main Menu St::Couldn't load font");
  26.     }
  27. }
  28.  
  29. void Settings::InitKeybinds()
  30. {
  31.     std::ifstream ifs("Config/MMKeys.ini");
  32.  
  33.     if (ifs.is_open()) {
  34.         std::string key = "";
  35.         std::string key2 = "";
  36.         int keyval = 0;
  37.         while (ifs >> key >> key2)
  38.         {
  39.             this->Keybinds[key] = this->SupportedKeys->at(key2);
  40.         }
  41.     }
  42.     ifs.close();
  43.  
  44.     this->Keybinds["CLOSE"] = this->SupportedKeys->at("ESC");
  45.     this->Keybinds["Left"] = this->SupportedKeys->at("A");
  46.     this->Keybinds["Right"] = this->SupportedKeys->at("D");
  47.     this->Keybinds["Up"] = this->SupportedKeys->at("W");
  48.     this->Keybinds["Down"] = this->SupportedKeys->at("S");
  49. }
  50.  
  51. void Settings::InitGui()//Lnk2019
  52. {
  53.     this->buttons["Exit"] = new gui::Button(
  54.         900.f, 880.f, 250.f, 50.f,
  55.         &this->Fnt, "Back", 50,
  56.         sf::Color(70, 70, 70, 200),
  57.         sf::Color(150, 150, 150, 250),
  58.         sf::Color(20, 20, 20, 50),
  59.         sf::Color(70, 70, 70, 0),
  60.         sf::Color(150, 150, 150, 0),
  61.         sf::Color(20, 20, 20, 0), sf::Color(70, 70, 70, 0));
  62.     std::string Li[] = {"1920x1080","800x600","640x480"};
  63.     this->drplist["Resolution"] = new gui::DrpList(800, 450, 200, 50, Fnt, Li, 3);
  64. }
  65.  
  66. Settings::Settings(sf::RenderWindow* window, std::map<std::string, int>* SupportedKeys, std::stack<State*>* states)
  67.     :State(window,SupportedKeys,states)
  68. {
  69.     this->InitVars();
  70.     this->InitBackGrnd();
  71.     this->InitFonts();
  72.     this->InitKeybinds();
  73.     this->InitGui();
  74. }
  75.  
  76. Settings::~Settings()
  77. {
  78.     auto it = this->buttons.begin();
  79.     for (it = this->buttons.begin(); it != this->buttons.end(); ++it) {
  80.         delete it->second;
  81.     }
  82.     auto it2 = this->drplist.begin();
  83.     for (it2 = this->drplist.begin(); it2 != this->drplist.end(); ++it2) {
  84.         delete it2->second;
  85.     }
  86. }
  87.  
  88.  
  89. //Access
  90. //Funcs 
  91. void Settings::UpdateInput(const float& dt)
  92. {
  93. }
  94.  
  95. void Settings::Update(const float& dt)
  96. {
  97.     this->UpdateMousePos();
  98.     this->UpdateInput(dt);
  99.  
  100.     this->UpdateGui(dt);
  101.  
  102.     std::cout << this->MousePosView.x << " " << this->MousePosView.y << "\r";
  103. }
  104.  
  105. void Settings::UpdateGui(const float& dt)
  106. {    //Update guis and handle their functions
  107.     for (auto& it : this->buttons) {
  108.         it.second->Update(this->MousePosView);
  109.     }
  110.     //Quit
  111.     if (this->buttons["Exit"]->Pressed()) {
  112.         this->Endstate();
  113.     }
  114.  
  115.     //Drop List
  116.     for (auto& it : this->drplist) {
  117.         it.second->Update(this->MousePosView, dt);
  118.     }
  119.  
  120. }
  121.  
  122. void Settings::RenderGui(sf::RenderTarget& target)
  123. {
  124.     for (auto& it : this->buttons) {
  125.         it.second->Render(target);
  126.     }
  127.  
  128.     for (auto& it : this->drplist) {
  129.         it.second->Render(target);
  130.     }
  131. }
  132.  
  133. void Settings::Render(sf::RenderTarget* target)
  134. {
  135.     if (!target)
  136.         target = this->window;
  137.     target->draw(this->BackGrnd);
  138.  
  139.     this->RenderGui(*target);
  140. }
The full error message:
'
Error LNK2019

unresolved external symbol "public: __thiscall gui::Button::Button(float,float,float,float,class sf::Font *,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,unsigned int,class sf::Color,class sf::Color,class sf::Color,class sf::Color,class sf::Color,class sf::Color,class sf::Color,class sf::Color,class sf::Color,unsigned short)" (??0Button@gui@@QAE@MMMMPAVFont@sf@@V?$basic_strin g@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@IV Color@3@22222222G@Z) referenced in function "private: void __thiscall Settings::InitGui(void)" (?InitGui@Settings@@AAEXXZ)

Project:
Star Fire

File:
F:\Star Fire SFML\Star Fire\Settings.obj

Line:
1
'


Everything works perfectly before video 48,a Lnk2019 occurs right after i put more parameters into the 'gui::Button();' constructor in 'Gui.cpp' specifically the 'sf::color::' OutlIdle - Hover and Active parameters. As soon as i put them in the error occurs.

Can someone help me ?
1 Week Ago #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.