-3

我编写了一个Dice模仿真实骰子行为的小类和可以继承的模板SingletonDice。我已经operator<<为类写了,Dice但不知何故编译器在找到它时遇到了问题。我为 , 重载了<<运算符Dice,它是从某些方法返回的,拥有它很方便。Sinlgeton<Dice>std::vector<int>Dice

我在 ubuntu 上使用Qt creator 2.5with 。gcc 4.7

/home/USER/programming/cpp_yahtzee/main.cpp:12: 错误: 'std::operator<< >(((* & std::cout), ((const char*) "你好")) << (& Singleton::Instance())->Dice::getLastThrow()'</p>

这是产生此错误的代码:

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

编辑 然而,这输出了预期的结果,完全没有错误: std::cout << Dice::Instance() 也许这是我的编译器的问题gcc/g++ 4.7(尝试过gcc/g++ 4.6.3并且效果相同)?

我的单人班

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();
        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;
}

现在我不能做这样的事情:

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

编辑 Ilya Lavrenov 的方法有效,尽管这不是我想要的,因为这需要创建一个局部变量。我在课堂上的某个地方有问题Singleton

4

3 回答 3

0
#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;
}

对您的代码进行了一些更改,现在它可以很好地编译。运算符 << 也很好用

于 2012-07-17T12:26:42.027 回答
0

它与错字Dice::Instane->有什么关系Dice::Instance吗?

于 2012-07-17T12:33:43.620 回答
-3

有时 IDE 会解释一些 'const' 运算符,例如 eclipse 和 virtual studio。jsut 从参数“const”中删除

std::ostream& operator<<(std::ostream& os, std::vector<int>& vect)

我认为它很有用,如果没有,请告诉我您使用的编译器版本和 IDE。

于 2012-07-17T12:24:49.000 回答