Thread: Help with a singleton class

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    228

    Help with a singleton class

    Hi.
    I'm trying to create a singleton class that will save radio statistics.
    here's how it looks:

    Code:
    #include<iostream>
    #include<string>
    #include<map>
    #include<iterator>
    #include<algorithm>
    #include"Song.h"
    #include"Playlist.h"
    using namespace std;
    
    
    
    class RadioStatistics
    {
    private:
        static RadioStatistics* m_instance;
        static map<string, int> m_songs;
        static map<string, int> m_singers;
        RadioStatistics();
    public:
        static RadioStatistics* getInstance();
        string getMostPlayedSong();
        string getMostPlayedSinger();
    };
    the idea is to increment the value of m_songs and m_singers whenever a song is played in the radio, so later it'll be possible to know what song was played the most and who's the most popular singer.

    here's some of the implementation so far:

    Code:
    #include "RadioStatistics.h"
    
    
    RadioStatistics::RadioStatistics()
    {
        cout<<"CTR called"; //indicator
    }
    
    
    RadioStatistics* RadioStatistics::getInstance()
    {
        if (!m_instance) //an error here
            m_instance=new RadioStatistics; 
        return m_instance;
    }
    
    
    bool CompareValues(pair<string, int> lhs, pair<string, int> rhs)
    {
        return lhs.second<rhs.second;
    }
    
    
    string RadioStatistics::getSongMostPlayed()
    {
        pair<string, int> tempPair=*max_element(m_songs.begin(),m_songs.end(),CompareValues); //an error here
        return tempPair.first;
    }
    the problem is:

    undefined reference to `RadioStatistics::m_instance'
    undefined reference to `RadioStatistics::m_instance'
    undefined reference to `RadioStatistics::m_instance'
    undefined reference to `RadioStatistics::m_songs'
    undefined reference to `RadioStatistics::m_songs'

    can anyone please shed some light on that issue?
    why is it undefined?

    thank you in advanced.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You have only declared the static members minstance, m_songs_m_singers. You have to define them as well in the .cpp
    e.g.
    Code:
    map<string, int>  RadioStatistics::m_songs;
    ...
    Kurt

  3. #3
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    You don't really have a "Singleton". You just have a collection of global variables in the `RadioStatistics' namespace.

    That said, why do you need a "Singleton"?

    Search for using `static' variables in a class.

    Soma

  4. #4
    Registered User
    Join Date
    May 2013
    Posts
    228
    Quote Originally Posted by ZuK View Post
    You have only declared the static members minstance, m_songs_m_singers. You have to define them as well in the .cpp
    e.g.
    Code:
    map<string, int>  RadioStatistics::m_songs;
    ...
    Kurt
    why is it different from when I define a data members within any other class?


    Quote Originally Posted by phantomotap View Post
    O_o

    You don't really have a "Singleton". You just have a collection of global variables in the `RadioStatistics' namespace.

    That said, why do you need a "Singleton"?

    Search for using `static' variables in a class.

    Soma
    I need a singleton because these are the assignment requirements...

  5. #5
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    why is it different from when I define a data members within any other class?
    O_o

    Why would you expect a `static' class variable, a variable disconnected from any instance, to be the same as a class member variable?

    I need a singleton because these are the assignment requirements...
    The assignment makes me sad. ;_;

    I guess you have to do the assignment, but keep this in mind for the future: generally speaking, you should just create a single instance of a class if you only need one.

    Soma

  6. #6
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Absurd View Post
    why is it different from when I define a data members within any other class?
    Because there is supposed to be only a single instance.
    Kurt

  7. #7
    Registered User
    Join Date
    May 2013
    Posts
    228
    Quote Originally Posted by phantomotap View Post
    The assignment makes me sad. ;_;
    you and me both.

    Quote Originally Posted by phantomotap View Post
    I guess you have to do the assignment, but keep this in mind for the future: generally speaking, you should just create a single instance of a class if you only need one.

    Soma
    and this is exactly what I need to achieve here.
    It actually makes sense to have a single instance of an object that suppose to gather statistics for the whole program.


    Quote Originally Posted by ZuK View Post
    Because there is supposed to be only a single instance.
    Kurt
    How do I define it then?
    What is the sintax?

    edit...

    I found this to be useful:
    Code:
    RadioStatistics* RadioStatistics::m_instance=NULL;
    solved the error for m_instance, but what about m_songs and m_singers?

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Same thing. Initialize them to nullptr if your compiler supports C++11 (because NULL is evil).
    Make sure you properly delete those instances at the end of your program, too (or use smart pointers if your compiler supports C+11/TR1).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Absurd View Post
    but what about m_songs and m_singers?
    see #2
    Kurt

  10. #10
    Registered User
    Join Date
    May 2013
    Posts
    228
    Perfect!

    thank you so much.

    Elysia, our assignments checked with an automatic input/output system, and I don't know much about its compiler specifications,
    so I prefer not using any extensions at this moment.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Absurd View Post
    Elysia, our assignments checked with an automatic input/output system, and I don't know much about its compiler specifications,
    so I prefer not using any extensions at this moment.
    They are not extensions. They are part of the language.
    Unfortunately, not all compilers up to snuff, so features added more recently (or more specifically, recent standards) have not gotten implemented in all compilers.
    Of course, you are not required to use them. I am simply making you aware of that they exist.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    It actually makes sense to have a single instance of an object that suppose to gather statistics for the whole program.
    O_o

    Sure. It just doesn't make any sense for the object to be purpose build as a "Singleton".

    Think of expanding the project: say you want to have a "most played song of the day" or "most played song of the week".

    Don't you think it would be silly to make a `RadioStatistics' for each frame of time?

    That was my point. You don't need a "Singleton" to only have a single instance or a globally accessible instance. The compiler isn't going to go around creating objects you don't ask to be created. If you only need a single instance, just create a single instance of an object and use it.

    Soma

  13. #13
    Registered User
    Join Date
    May 2013
    Posts
    228
    Quote Originally Posted by phantomotap View Post
    O_o

    Sure. It just doesn't make any sense for the object to be purpose build as a "Singleton".

    Think of expanding the project: say you want to have a "most played song of the day" or "most played song of the week".

    Don't you think it would be silly to make a `RadioStatistics' for each frame of time?

    That was my point. You don't need a "Singleton" to only have a single instance or a globally accessible instance. The compiler isn't going to go around creating objects you don't ask to be created. If you only need a single instance, just create a single instance of an object and use it.

    Soma
    what would be an appropriate use of a Singleton then?

  14. #14
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    People debate the usefulness of the singleton pattern as a whole, mainly because the singleton is overused, and not entirely necessary. The singleton is just a wrapper around a global instance 99% of the time. The whole point of criticism is that it isn't useful.

  15. #15
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    what would be an appropriate use of a Singleton then?
    O_o

    I'm not sure that any real cases for "Singleton" classes exist, but if any do exist, they would certainly be very specific cases, and even then, the need for a class to be built as a "Singleton" is imaginary as what you've done in building a "Singleton" is easily isolated allowing you to get a normal class and a "Singleton" facility at the same time.

    Code:
    static SNormalObject * g = 0;
    SNormalObject * GetNormalObject()
    {
        MLazy<SNormalObject>::type s(g);
        return(g);
    }
    I will not give you code, but the point is that `MLazy' is a facility that lazily constructs a `SNormalObject' on behalf of `GetNormalObject'.

    If you need a normal instance, you can use `SNormalObject' like a normal class.

    If you need the globally accessible object, without "C++ Order Fiasco" problems, use `GetNormalObject' as you would for a `Singleton'.

    Even this sort of thing is rarely necessary, but it is valuable in that it solves real problems without purpose building "Singleton" classes.

    Code:
    class SObject
    {
        // ...
        SObject
        (
            /**/
          , SNormalObject * fObject = GetNormalObject()
        );
        // ...
    };
    Code:
    SObject g1; // This just works; the `GetNormalObject' is known to "live" an appropriate "life".
    The point is, you haven't built a "Singleton"; you've created a single instance of an object which you are just using from a global point of access.

    The idea is barely different than a "Singleton" class from client perspective, but if you do ever need another instance, you can just go ahead and create one.

    Soma

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class vs. Namespace as Singleton
    By eros in forum C++ Programming
    Replies: 0
    Last Post: 03-28-2012, 06:07 AM
  2. Singleton Class error
    By anirban in forum C++ Programming
    Replies: 10
    Last Post: 10-28-2010, 06:44 AM
  3. Compiler complaining about my singleton class
    By DavidP in forum C++ Programming
    Replies: 12
    Last Post: 06-09-2009, 02:30 AM
  4. Bug in Singleton class
    By marquito in forum C++ Programming
    Replies: 17
    Last Post: 12-14-2007, 01:02 PM
  5. singleton class problem
    By ... in forum C++ Programming
    Replies: 6
    Last Post: 12-22-2003, 06:16 PM