SFML community forums

Help => General => Topic started by: vcjr on January 04, 2014, 01:44:10 am

Title: Getting Error when creating a Singleton class
Post by: vcjr 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 http://www.youtube.com/watch?v=Q7O13p_IY0k&list=SPCB3138ADCE90F2EC&index=1 (http://www.youtube.com/watch?v=Q7O13p_IY0k&list=SPCB3138ADCE90F2EC&index=1)

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.
Title: Re: Getting Error when creating a Singleton class
Post by: Daddi on January 04, 2014, 01:47:15 am
static ScreenManager GetInstance();

You missed an ampersand (&) inside your header file ;)
Title: Re: Getting Error when creating a Singleton class
Post by: vcjr on January 04, 2014, 01:50:52 am
This might sound dumb but what exactly ampersand (&) do?

Thank you for last responce, worked.
Title: Re: Getting Error when creating a Singleton class
Post by: Daddi on January 04, 2014, 01:52:34 am
It defines, that the returned ScreenManager instance will be a reference :)
Title: Re: Getting Error when creating a Singleton class
Post by: vcjr on January 04, 2014, 01:53:38 am
Okay, thanks you again :D
Title: Re: Getting Error when creating a Singleton class
Post by: iride on January 04, 2014, 02:22:24 am
singletons are bad. don't use them
Title: Re: Getting Error when creating a Singleton class
Post by: Daddi 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 ;)
Title: Re: Getting Error when creating a Singleton class
Post by: vcjr 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.
Title: Re: Getting Error when creating a Singleton class
Post by: Nexus 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 (http://en.sfml-dev.org/forums/index.php?topic=5187.msg34227#msg34227) for a detailed explanation of the problems that Singleton brings.

Concerning the quality of CodingMadeEasy tutorials, I've just stated my opinion here (http://en.sfml-dev.org/forums/index.php?topic=14033.msg98306#msg98306).
Title: Re: Getting Error when creating a Singleton class
Post by: amir ramezani 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
Title: Re: Getting Error when creating a Singleton class
Post by: eXpl0it3r 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.