Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8246551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:32:32+00:00 2026-06-07T22:32:32+00:00

I have written a small class Dice that imitates the behavior of real dice

  • 0

I have written a small class Dice that imitates the behavior of real dice and templated class Singleton that Dice can inherit from. I have written operator<< for class Dice but somehow compiler is having problems with finding it. I have overloaded << operators for Dice , Sinlgeton<Dice> and std::vector<int> which is returned from some Dice methods and it’s handy to have it.

I use Qt creator 2.5 with gcc 4.7 on ubuntu.

/home/USER/programming/cpp_yahtzee/main.cpp:12: error: no match for
‘operator<<’ in ‘std::operator<< >((* &
std::cout), ((const char*)”hello”)) << (&
Singleton::Instance())->Dice::getLastThrow()’

and this is the codes that produces this error :

std::cout << "hello" << Dice::Instance().getLastThrow();

EDIT
Yet this outputs what expected with no error at all :
std::cout << Dice::Instance()
Maybe that’s a problem with my compiler gcc/g++ 4.7 (tried gcc/g++ 4.6.3 and the effect is the same) ?

My sinlgeton class

template <typename T>
class Singleton
{
public:
    static T& Instance();
    Singleton() {}
private:

    //declare them to prevent copies
    Singleton(Singleton const&);
    void operator=(Singleton const&);

};

template<typename T>
T& Singleton<T>::Instance()
{
    static T _instance;
    return _instance;
}

Dice class :

    class Dice : public Singleton<Dice>
    {
    private:
        std::vector<int> _lastThrow;
    public:
        Dice();
        std::vector<int> generateThrow();
        friend std::ostream& operator<<(std::ostream& os, const Dice& dice);
        friend std::ostream& operator<<(std::ostream& os, const Singleton<Dice>& dice);
        friend std::ostream& operator<<(std::ostream& os, const std::vector<int>& vect);

        //accessor method - returning last throw
        const std::vector<int>& getLastThrow();

        //rethrowing {1,4} - dice #1 and #4
        std::vector<int> Rethrow(const std::vector<int>& objects);
    };

std::ostream& operator<<(std::ostream& os, const Dice& dice)
{
    for (std::vector<int>::const_iterator it = dice._lastThrow.begin();  it != dice._lastThrow.end(); ++it) {
        os << *it;
    }
    return os;
}
std::ostream& operator<<(std::ostream& os, const Singleton<Dice>& dice)
{
    for (std::vector<int>::const_iterator it = dice.Instance().getLastThrow().begin();  it != dice.Instance().getLastThrow().end(); ++it) {
        os << *it;
    }
    return os;

}

std::ostream& operator<<(std::ostream& os, const std::vector<int>& vect)
{
    for (std::vector<int>::const_iterator it = vect.begin();  it != vect.end(); ++it) {
        os << *it;
    }
    return os;
}

std::vector<int> Dice::generateThrow()
{
    static std::vector<int> v(5);

    for (std::vector<int>::iterator it = v.begin();  it != v.end(); ++it) {
        (*it) = rand()%(DICE_MAX)+1;
    }
    _lastThrow = v;
    return v;
}

Now I cannot do something like this :

std::cout << Dice::Instance().generateThrow();

EDIT
Ilya Lavrenov’s method is working although this is not what I want because this requires creating a local variable. I have a problem somewhere with the Singleton class.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T22:32:34+00:00Added an answer on June 7, 2026 at 10:32 pm
    #include <iostream>
    #include <vector>
    
    template <typename T>
    class Singleton
    {
    public:
        static T& Instance();
        Singleton() {}
    private:
    
        //declare them to prevent copies
        Singleton(Singleton const&);
        void operator=(Singleton const&);
    
    };
    
    template<typename T>
    T& Singleton<T>::Instance()
    {
        static T _instance;
        return _instance;
    }
    
    class Dice : public Singleton<Dice>
    {
    private:
        std::vector<int> _lastThrow;
    public:
        Dice()
        {
            for (int i = 0; i < 10; ++i)
                _lastThrow.push_back(i);
        }
        std::vector<int> generateThrow();
    
        //accessor method - returning last throw
        const std::vector<int>& getLastThrow()
        {
            return _lastThrow;
        }
    
        //rethrowing {1,4} - dice #1 and #4
        std::vector<int> Rethrow(const std::vector<int>& objects);
    };
    
    std::ostream& operator<<(std::ostream& os, const Dice& dice)
    {
        for (std::vector<int>::const_iterator it = dice.Instance().getLastThrow().begin();  it != dice.Instance().getLastThrow().end(); ++it) {
            os << *it;
        }
        return os;
    }
    std::ostream& operator<<(std::ostream& os, const Singleton<Dice>& dice)
    {
        for (std::vector<int>::const_iterator it = dice.Instance().getLastThrow().begin();  it != dice.Instance().getLastThrow().end(); ++it) {
            os << *it;
        }
        return os;
    
    }
    
    std::ostream& operator<<(std::ostream& os, const std::vector<int>& vect)
    {
        for (std::vector<int>::const_iterator it = vect.begin();  it != vect.end(); ++it) {
            os << *it;
        }
        return os;
    }
    
    std::vector<int> Dice::generateThrow()
    {
        static std::vector<int> v(5);
    
        for (std::vector<int>::iterator it = v.begin();  it != v.end(); ++it) {
            (*it) = rand()%(354535)+1;
        }
        _lastThrow = v;
        return v;
    }
    
    int main()
    {
        Singleton<Dice> a;
        std::cout << a << std::endl;
    
        return 0;
    }
    

    Some changes of your code and now its compiles well. And operator << also works well

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a small class, which reads out annotation from methods. Now I
I have written a small wrapper for the dict built-in class that loads entries
I have written a small util in an app that syncs the time from
I have written a small chat app that uses mysql + php to facilitate
I have written a small app that puts my bluetooth in discoverable mode for
I have written a small makefile for a few simple C programs that compiles
I have written a Java applet class and made a small HTML page to
I have written some code that uses attributes of an object: class Foo: def
We have a small framework written in C# .Net 2.0 that we want to
I have written a small custom class to run an audit trail in Lotus

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.