有人能告诉我为什么我在Duck类的“setFlyBehaviour”和“setQuackBehaviour”中出现编译错误? (错误:“术语不会评估为带有1个参数的函数”)
这是Head First Design Patterns(我在Java中用C ++翻译)的策略模式示例。我介绍了一个Wrapper模式来管理内存。 (我使用的包装类来自Mark Joshi,期权定价和设计模式)
感谢!!!!!
#include <iostream>
#include <algorithm>
#include <math.h>
#include <string>
#include <map>
#include <exception>
#include <vector>
using namespace std;
template< class T>
class Wrapper
{
//Taken From Mark Joshi, Designs patterns and derivatives pricing
public:
Wrapper()
{ DataPtr =0;}
Wrapper(const T& inner)
{
DataPtr = inner.clone();
}
Wrapper(const Wrapper<T>& original)
{
if (original.DataPtr !=0)
DataPtr = original.DataPtr->clone();
else
DataPtr=0;
}
~Wrapper()
{
if (DataPtr !=0)
delete DataPtr;
}
Wrapper& operator=(const Wrapper<T>& original)
{
if (this != &original)
{
if (DataPtr!=0)
delete DataPtr;
DataPtr = (original.DataPtr !=0) ? original.DataPtr->clone() : 0;
}
return *this;
}
T& operator*()
{
return *DataPtr;
}
const T& operator*() const
{
return *DataPtr;
}
const T* const operator->() const
{
return DataPtr;
}
T* operator->()
{
return DataPtr;
}
private:
T* DataPtr;
};
/*****************************************************/
/***************** Interfaces ****************/
/*****************************************************/
class FlyBehaviour
{
private:
public:
virtual void fly() const = 0;
virtual FlyBehaviour* clone() const = 0;
};
class QuackBehaviour
{
private:
public:
virtual void quack() const = 0;
virtual QuackBehaviour* clone() const = 0;
};
/*****************************************************/
/***************** Implementations ***********/
/*****************************************************/
// -- FlyBehaviour
class FlyWithWings : public FlyBehaviour
{
public:
virtual void fly() const{
cout << "flying with wings" << endl;
}
virtual FlyBehaviour* clone() const {
return new FlyWithWings;
}
};
class FlyNoWay : public FlyBehaviour
{
public:
virtual void fly() const{
cout << "flying no way" << endl;
}
virtual FlyBehaviour* clone() const {
return new FlyNoWay;
}
};
// -- QuackBehaviour
class Quack : public QuackBehaviour
{
public:
virtual void quack() const{
cout << "Quacking here!" << endl;
}
virtual QuackBehaviour* clone() const{
return new Quack;
}
};
class Squeak : public QuackBehaviour
{
public:
virtual void quack() const{
cout << "Squeaking here!" << endl;
}
virtual QuackBehaviour* clone() const{
return new Squeak;
}
};
class Duck
{
private:
Wrapper<FlyBehaviour> flyBehaviour;
Wrapper<QuackBehaviour> quackBehaviour;
public:
void performQuack() const{
quackBehaviour->quack();
}
void performFly() const{
flyBehaviour->fly();
}
void setFlyBehaviour(const FlyBehaviour& mfly){
flyBehaviour(mfly);
}
void setQuackBehaviour(const FlyBehaviour& mquack){
quackBehaviour(mquack);
}
virtual void display() const{
}
};
class MallardDuck : public Duck
{
public:
virtual void display() const{
cout << "It looks like a Mallar" << endl;
}
};
class RedheadDuck : public Duck
{
public:
virtual void display() const{
cout << "It looks like a Redhead" << endl;
}
};
int main()
{
}
答案 0 :(得分:0)
失败,因为"hits": {
"total": 1,
"max_score": 0.30685282,
"hits": [
{
"_index": "special",
"_type": "character",
"_id": "AU_gcWOBn6s1TzYZvmTv",
"_score": 0.30685282,
"_source": {
"d2": "*"
}
}
]
}
不提供函数调用操作符:
Wrapper
您正试图拨打R T::operator ()(Arg1 a1, Arg2 a2, …)
模板:
Wrapper
答案 1 :(得分:0)
你实际上是在尝试调用不存在的函数而不是构造函数。 你传递给setQuackBehaviour的争论中也有错误。
这应该有效:
void setFlyBehaviour(const FlyBehaviour& mfly){
flyBehaviour = mfly;
}
void setQuackBehaviour(const QuackBehaviour& mquack){
quackBehaviour = mquack;
}
答案 2 :(得分:0)
@werewindle是这样的吗?
void reset(const QuackBehaviour& original){
if (DataPtr!= &original)
{
if (DataPtr!=0)
delete DataPtr;
DataPtr = (original.DataPtr !=0) ? original.DataPtr->clone() : 0;
}
}