我写了一个小类Dice
来模仿真实骰子和Singleton
可以继承的模板类Dice
的行为。我已经为类operator<<
编写了Dice
但是编译器在找到它时遇到了问题。我为<<
,Dice
和Sinlgeton<Dice>
重置std::vector<int>
运算符,这些运算符是从某些Dice
方法返回的,并且它很方便。
我在ubuntu上使用Qt creator 2.5
和gcc 4.7
。
/home/USER/programming/cpp_yahtzee/main.cpp:12:错误:不匹配 '运营商&lt;&lt;'在'std :: operator&lt;&lt; &gt;((*&amp; std :: cout),((const char *)“hello”))&lt;&lt; (安培; 单::实例()) - &GT;骰子:: getLastThrow()”
这是产生此错误的代码:
std::cout << "hello" << Dice::Instance().getLastThrow();
修改
然而,这会输出预期完全没有错误:
std::cout << Dice::Instance()
也许这是我的编译器gcc/g++ 4.7
的问题(试过gcc/g++ 4.6.3
,效果是一样的)?
我的sinlgeton班
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
类的某个地方遇到了问题。
答案 0 :(得分: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;
}
您的代码有些变化,现在编译得很好。和运算符&lt;&lt;也很好用
答案 1 :(得分:0)
是否与拼写错误有关Dice::Instane
- &gt; Dice::Instance
?
答案 2 :(得分:-3)
有时IDE intrepret不同于某些'const'运算符,例如eclipse和virtual studio。 jsut从参数'const'
中删除std::ostream& operator<<(std::ostream& os, std::vector<int>& vect)
我认为它很有用,如果没有,请告诉我您使用的编译器版本和IDE。