Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Getting Error when creating a Singleton class  (Read 4166 times)

0 Members and 1 Guest are viewing this topic.

vcjr

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Getting Error when creating a Singleton class
« on: January 04, 2014, 01:44:10 am »
So I'm fallowing the CodingMadeEasy Platformer tutorial, but I have been doing sfml for a couple of days now so I know the syntax between 1.6 and 2.1 wont be much of a problem. But what is really getting to me is this problem.

Here is the Code First:
 ScreenManager.h
#ifndef SCREENMANGER_H
#define SCREENMANGER_H
#pragma once

#include <string>
#include <iostream>
class ScreenManager
{
public:
       
        ~ScreenManager();
        static ScreenManager GetInstance();

        void SetText(std::string text);
        void DrawText();
private:
        ScreenManager();
        ScreenManager(ScreenManager const&);
        void operator = (ScreenManager const&);

        std::string text;

};

#endif

 

ScreenManager.cpp
#include "ScreenManager.h"

ScreenManager &ScreenManager::GetInstance()
{
        static ScreenManager instance;
                return instance;
}

ScreenManager::ScreenManager()
{
}


ScreenManager::~ScreenManager()
{
}

 

The link to the video is

The compiler is telling me
Error   1       error LNK2019: unresolved external symbol "public: static class ScreenManager __cdecl ScreenManager::GetInstance(void)" (?GetInstance@ScreenManager@@SA?AV1@XZ) referenced in function "void __cdecl `dynamic initializer for 'sm''(void)" (??__Esm@@YAXXZ)     C:\Users\Vcjr\documents\visual studio 2013\Projects\Pwn or Die Trying\Pwn or Die Trying\ScreenManager.obj       Pwn or Die Trying

        3       IntelliSense: declaration is incompatible with "ScreenManager ScreenManager::GetInstance()" (declared at line 12 of "c:\users\vcjr\documents\visual studio 2013\projects\pwn or die trying\pwn or die trying\ScreenManager.h")  c:\Users\Vcjr\Documents\Visual Studio 2013\Projects\Pwn or Die Trying\Pwn or Die Trying\ScreenManager.cpp       3       31      Pwn or Die Trying

 

I know that c++ code could be outdate but I'm also very new to making classes and such.

Daddi

  • Jr. Member
  • **
  • Posts: 66
    • View Profile
    • http://foxdev.de/
    • Email
Re: Getting Error when creating a Singleton class
« Reply #1 on: January 04, 2014, 01:47:15 am »
static ScreenManager GetInstance();

You missed an ampersand (&) inside your header file ;)
« Last Edit: January 04, 2014, 01:50:42 am by Daddi »

vcjr

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: Getting Error when creating a Singleton class
« Reply #2 on: January 04, 2014, 01:50:52 am »
This might sound dumb but what exactly ampersand (&) do?

Thank you for last responce, worked.

Daddi

  • Jr. Member
  • **
  • Posts: 66
    • View Profile
    • http://foxdev.de/
    • Email
Re: Getting Error when creating a Singleton class
« Reply #3 on: January 04, 2014, 01:52:34 am »
It defines, that the returned ScreenManager instance will be a reference :)

vcjr

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: Getting Error when creating a Singleton class
« Reply #4 on: January 04, 2014, 01:53:38 am »
Okay, thanks you again :D

iride

  • Jr. Member
  • **
  • Posts: 88
    • View Profile
    • Email
Re: Getting Error when creating a Singleton class
« Reply #5 on: January 04, 2014, 02:22:24 am »
singletons are bad. don't use them

Daddi

  • Jr. Member
  • **
  • Posts: 66
    • View Profile
    • http://foxdev.de/
    • Email
Re: Getting Error when creating a Singleton class
« Reply #6 on: January 04, 2014, 02:28:51 am »
Thats not something you should tell someone who just learns the language. Good and bad practices are something to learn if you are an intermediate, not a beginner. He has to know and understand first what singletons are before he can learn how and why to avoid them ;)

vcjr

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: Getting Error when creating a Singleton class
« Reply #7 on: January 04, 2014, 02:35:09 am »
:D The Guy Explained on the Video on why its bad, but it had a good use for what the tutorial was going to use them for.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Getting Error when creating a Singleton class
« Reply #8 on: January 04, 2014, 10:14:30 am »
Thats not something you should tell someone who just learns the language.
Singletons are even less something you should tell someone who just learns the language. They're an advanced design pattern with very specific and rare use cases, and definitely not the default approach. Unfortunately, many people overuse it without caring about the consequences. See here for a detailed explanation of the problems that Singleton brings.

Concerning the quality of CodingMadeEasy tutorials, I've just stated my opinion here.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

amir ramezani

  • Jr. Member
  • **
  • Posts: 81
  • i'm a programmer who can't see well
    • View Profile
    • download useful software!
    • Email
Re: Getting Error when creating a Singleton class
« Reply #9 on: January 04, 2014, 10:30:32 am »
everything has pros and cons
singletons are of course good, but you have to use it in the correct way
if singleton hasn't any pros, it would not been added in the language at any time
if you can't see well, you can't test your applications and operating system well
my game engine:
allegro game creator
my operating system:
AmirOS

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Getting Error when creating a Singleton class
« Reply #10 on: January 04, 2014, 11:18:59 am »
There are use cases, but you essentially always choose a different design and those alternatives are mostly always better than a Singleton.

Also Singletons are not a language feature. It's just that C++ is very flexible and allows for nearly anything one can imagine.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything